anonymous-types

How should anonymous types be used in C#?

I've seen lots of descriptions how anonymous types work, but I'm not sure how they're really useful. What are some scenarios that anonymous types can be used to address in a well-designed program? ...

linq equivalent of 'select *' sql for generic function?

I've got a generic<> function that takes a linq query ('items') and enumerates through it adding additional properties. How can I select all the properties of the original 'item' rather than the item itself (as the code below does)? So equivalent to the sql: select *, 'bar' as Foo from items foreach (var item in items) { var newIte...

Other than for LINQ queries, how do you use anonymous types in C#?

I've been trying to get up to speed on some of the newer features in C# and one of them that I haven't had occasion to use is anonymous types. I understand the usage as it pertains to LINQ queries and I looked at this SO post which asked a similar question. Most of the examples I've seen on the net are related to LINQ queries, which is...

Can a C# anonymous class implement an interface?

Is it possible to have an anonymous type implement an interface. I've got a piece of code that I would like to work, but don't know how to do this. I've had a couple of answers that either say no, or create a class that implements the interface construct new instances of that. This isn't really ideal, but I'm wondering if there is a mec...

Explaining Anonymous Types to people used to Variants

For the last year and a half we've gotten been working on a large WPF application and it's finally exiting beta and moving to full release. When we started the project, I fought hard for every assembly to use Option Strict. My team had little .NET experience prior to this project, and alot of experience with Visual Basic 5 where nearly e...

Can you Instantiate an Object Instance from JSON in .NET?

Since Object Initializers are very similar to JSON, and now there are Anonymous Types in .NET. It would be cool to be able to take a string, such as JSON, and create an Anonymous Object that represents the JSON string. Use Object Initializers to create an Anonymous Type: var person = new { FirstName = "Chris", LastName = "Johns...

C# 3.0 Anonymous Types: Naming

I was wondering if there is some way to name or rename a property on an Anonymous type to include a space in the property name. For example: var resultSet = from customer in customerList select new { FirstName = customer.firstName; }; In this example I would like FirstName to be "First Name". The reason for this ...

How .NET 3.5 (lambdas, Linq) evolved

I remember reading a post about a year or so ago by Scott Hanselman (maybe by scott guthrie) about how Linq evolved. It remember it showing that anyonymous types led to lambda expressions which then led to Linq in some way. I can't seem to find it on google. Does anyone else remember this post? If so, can you please post it? ...

help with Anonymous Types in a signature

Hi all, I am trying to get the signature on the method below to work. As this is an Anonymous Type I have some trouble, any help would be great. When I looked at sortedGameList.ToList() in a QuickWatch window I get the signature "System.Collections.Generic.List<<>f__AnonymousType0>>" Many Thanks Donald public List<IGrouping<Dat...

LINQ to XML optional element query

I'm working with an existing XML document which has a structure (in part) like so: <Group> <Entry> <Name> Bob </Name> <ID> 1 </ID> </Entry> <Entry> <Name> Larry </Name> </Entry> </Group> I'm using LINQ to XML to query the XDocument to retrieve all these entries as follows: var items = from g in...

Create Generic Class instance based on Anonymous Type

Hi, I have a class ReportingComponent<T>, which has the constructor: public ReportingComponent(IQueryable<T> query) {} I have Linq Query against the Northwind Database, var query = context.Order_Details.Select(a => new { a.OrderID, a.Product.ProductName, a.Order.OrderDate }); Query is of type IQueryable<a'>, where ...

Iterating through anonymous typed data in MVC view

Into some view data i have put the result of an anonymous type: var projectData = from p in db.Projects orderby p.title select new { Title = p.title, DevURL = p.devURL ?? "N/A", ...

Anonymous Types - Are there any distingushing characteristics?

Is there anything to use, to determine if a type is actually a anonymous type? For example an interface, etc? The goal is to create something like the following... //defined like... public static T Get<T>(this IAnonymous obj, string prop) { return (T)obj.GetType().GetProperty(prop).GetValue(obj, null); } //... //And then used like...

(Extension) Methods and Anonymous Types

Hi, I know that you cannot return anonymous types from methods but I am wondering how the Select extension method returns an anonymous type. Is it just a compiler trick? Edit Suppose L is a List. How does this work? L.Select(s => new { Name = s }) The return type is IEnumerable<'a> where 'a = new {String Name} ...

How do I serialize a C# anonymous type to a JSON string?

I'm attempting to use the following code to serialize an anonymous type to JSON: var serializer = new DataContractJsonSerializer(thing.GetType()); var ms = new MemoryStream(); serializer.WriteObject(ms, thing); var json = Encoding.Default.GetString(ms.ToArray()); However, I get the following exception when this is executed: Type ...

Accessing constructor of an anonymous class

Lets say I have a concrete class Class1 and I am creating an anonymous class out of it. Object a = new Class1(){ void someNewMethod(){ } }; Now is there any way I could overload the constructor of this anonymous class. Like shown below Object a = new Class1(){ void someNewMethod(){ } publ...

What is the syntax for an Anonymous type in VB?

i am trying to set the htmlAttribute property but i cant figure out the correct syntax here is how it works in c# <%=Html.Password("myPassword", 50,"",new { style = "width:100px" })%><br /> what would be the vb.net version of new { style = "width:100px" } ? ...

Build objects with variable number of members?

In my asp.net mvc page, I want to call a RedirectToAction(actionName, controllerName, values). The values parameter is an object that contains all the nessecary values. Example return RedirectToAction(redirectAction, redirectController, new{ personId = foundId, personEmail = foundEmail, personH...

Anonymous Type vs Dynamic Type

What are the real differences between anonymous type(var) in c# 3.0 and dynamic type(dynamic) that is coming in c# 4.0? ...

LINQ Anonymous Types + MVC Views Help

I've seen many questions about this, but i've never really got the answer that I need. I'm converting a fairly large web application from Web Forms to MVC and after a while I encountred a problem with passing data to the view. In the Action I execute the code: //This is just an example ViewData["QProducts"] = from p in db.Products s...