anonymous-types

A simple and succinct definiton and explanation of anonymous types in C#?

I have no clue what an "anonymous type" is in C# nor how it is used. Can somone give me a good description of it and it's use? [Note: i really know what it is and how to use it but thought i'd ask for those that don't] ...

.net 3.5 anonymous foreach

I'm trying to loop through the results of a function that is returning an anonymous object of results. public static object getLogoNav() { XDocument loaded = XDocument.Load(HttpContext.Current.Request.MapPath("~/App_Data/LOGO_NAV_LINKS.xml")); var query = from x in loaded.Elements().Elements() select new ...

Dynamically set the property name of a C# anonymous type

Is there any way to dynamically set the property name of an anonymous type? Normally we'd do like this: var anon = new { name = "Kayes" }; Now I'd like to set the name (or identifier) of the property dynamically, so that this name can come from an XML file or a database. ...

Determining whether a Type is an Anonymous Type

In C# 3.0, is it possible to determine whether an instance of Type represents an Anonymous Type? ...

When selecting an anonymous type with LINQ from EF, is there no way to run a method on an object as you select it?

Let's say I have a method: bool myMethod(int a) { //return a bool } So let's say I the following // assume a has prop1 and prop2 both ints var mySelection = from a in myContainer where a=somecondition select new { a.prop1, myMethod(a.prop2) ...

Problems with Linq using anonymous type

Why did the anonymous type property "Points" still have the value "0"? Public Class Test Public Sub New(ByVal _ID As Integer) ID = _ID End Sub Public ID As Integer End Class Dim list As New List(Of Test) list.Add(New Test(1)) list.Add(New Test(2)) list.Add(New Test(3)) Dim query = From X In list Select New With {....

Entity Framework: How to partially populate an Entity

When creating a query with EF, Normally we will create an anonymous type in order to limit the number of columns returned. But anonymous type cannot be returned or used as a parameter to a method call, which means all work related to that anonymous object should be done inside a single method. This is really bad. And certainly, we don'...

Silverlight security: giving a permission to access anonymous classes to a class library

I'm porting an existing class library to Silverlight. I used lambda expression compilation a lot and now I'm experiencing security problems because of it. In particular, if an anonymous class from a client SL app is participating in a lambda expression, I cannot compile it: I get a MethodAccessException with the following stack trace: ...

Anonymous collection initializer for a dictionary

Is it possible to implicitly declare next Dictionary<HyperLink, Anonymous>: { urlA, new { Text = "TextA", Url = "UrlA" } }, { urlB, new { Text = "TextB", Url = "UrlB" } } so I could use it this way: foreach (var k in dic) { k.Key.Text = k.Value.Text; k.Key.NavigateUrl = k.Value.Url; } ? ...

Iterate through IQueryable of no specific type?

So I have this LINQ query that ends in a custom select kinda like this: select new { this1 = table.this1, this2 = othertable.this2 } The call to that query from the Controller looks something like this: ViewData["these"] = theRepo.GetAllThese(someVar, anotherVar); Now when I pass this on to my view since it is not strongly typed ho...

Linq to DataTable without enumerating fields

Hi, i´m trying to query a DataTable object without specifying the fields, like this : var linqdata = from ItemA in ItemData.AsEnumerable() select ItemA but the returning type is System.Data.EnumerableRowCollection<System.Data.DataRow> and I need the following returning type System.Data.EnumerableRowCollection<<object,object>> (...

How to query Anonymous Type collection?

How do you query a collection which is populated/created with select new? I have this BindingSource: this.bindingSource.DataSource = from row in db.Table select new { name = row.Name + row.Num.ToString() }; I'd like to query it like I do with other BindingSources: var query = from row in (IEnumerable<Table>)a...

C# Anonymous Type

When I say Anonymous Type Declaration var someType = new { Name = "Jon Skeet", Age = 10 }; However the Keyword var is implicitly typed but when i print Response.Write(someType.GetType().Name); it produces <>f__AnonymousType02.What is this symbol <> relates to? ...

Iterating and creating new anonymous types dynamically

I have an anonymous type of this form: new List<MyList>() { new Column { Name="blah", Width=100, Hidden=true }, new Column { Name="blah1", Width=60, Hidden=false } } How can I go about creating the content within the list dynamically, like: new List<MyList>() { foreach (var columns in col) { new Column { Name=column...

LINQ distinct on class item?

Note this question is similar this one except I'm not working with linq-to-sql, so the "let" is not usable. Basically I have a select of the type ... .Select(c => new SampleClass { Id = c.Location.Name, Name = c.Location.Name }).Distinct().ToList() which used to work when I just had ... .Select(c => c.Location.Name).Distinct().ToLis...

How to return a generic list collection in C#?

Hi I have some linq to sql method and when it does the query it returns some anonymous type. I want to return that anonymous type back to my service layer to do some logic and stuff on it. I don't know how to return it though. I thought I could do this public List<T> thisIsAtest() { return query; } but I get this error Error...

List to Anonymous Type in c#

The short story: I want to convert a list/dictionary into an anonymous object Basically what I had before was: var model = from item in IEnumerable<Item> select new { name = item.Name value = item.Value } etc. If I have name, item.Name in a list or dictionary, how can I go about creating the same anonymous obje...

c#, what does method ( new { x=y.property } ) mean?

I've been watching some videos on asp.net MVC, I think this question is just a general C# one though. I notice sometimes in the example code, method paramters are called like this blah.method("something here", "Something else", new { blah=item.someProperty }); Can you explain what is happening for the 3rd para...

ASP.NET, C# and Anonymous Types - Iterate through a DataTable while Manually Building an Anonymous Type

Dear Group, I am currently implementing a client-side paging solution using ASP.NET, jQuery and JSON. I have been following the excellent article from encosia: http://encosia.com/2008/08/20/easily-build-powerful-client-side-ajax-paging-using-jquery/ In my Web Method I retrieve my data from the database as a DataTable: DataTable c...

Fill DetailsView.DataSource with anonymous type

I have an asp:DetailsView with several columns, e.g. Foo, Bar. I want to fill it with an anonymous type, i.e.: gridView.DataSource = new { Foo = 1, Bar = "2" }; gridVeew.DataBind(); But getting next error: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. How can I do what I wan...