anonymous-types

is order of field important in anonymous types automatic initialization?

Hi, I got a scenario to create the anonymous list from the anonymous types, and i achieved that using public static List<T> MakeList<T>(T itemOftype) { List<T> newList = new List<T>(); return newList; } static void Main(string[] args) { //anonymos type var xx = new { ...

Trouble with LINQ databind to GridView and RowDataBound

Greetings all, I am working on redesigning my personal Web site using VS 2008 and have chosen to use LINQ to create by data-access layer. Part of my site will be a little app to help manage my budget better. My first LINQ query does successfully execute and display in a GridView, but when I try to use a RowDataBound event to work with t...

How to convert an anonymously typed List to List<T>?

This simple Linq query: from c in mycontext.Customers join o in mycontext.Orders on c.CustomerId equals o.CustomerId where o.Status == 1 select new {c, o} will result in List<{c:Customer, o:Order}> after calling ToList(). What's the easiest way of converting this anonymously typed list into a list of customers (List<Customer>)? E...

Are anonymous types in c# accessible through reflection?

As the name of the anonymous type is compiler generated, so is it accessible through reflection? ...

Pass and receive anonymous objects between methods in C#

Consider a scenario where a block of code is trying to pass an anonymous object to another method in C#. Here's an example: ThreadPool.QueueUserWorkItem(new WaitCallback(RpvService.GetRpvDailyResults), new { req = request, rpvDic = rpvDictionary } ); How can you receive the ano...

Referring to non-final fields of an enclosing class inside an anonymous inner class in Java

In Java, I know that it is possible to do something like this: public class Greeter { public void greetEventually() { final String greeting = "Hello!"; Job j = new Job() { public void run() { System.out.println(greeting); } }; j.schedule(); } } This would ...

How to persist anonymous types instances through NHibernate?

Instinctively, I would say that this is impossible, as NHibernate needs to know some mapping information on how to persist a given type. Thus, I encountered some situations in which I could use such a feature. For instance, through a named query or something like so. On the other hand, using a named query would require me to make an addi...

How to get properties from anonymous type in VB.NET

I am trying to figure out how to get at an anonymous typed object's properties, when that anonymous type isn't created in the current function. Specifically, I am binding an ASP.NET ListView to LINQ resultset, then trying to process each item in the ItemDataBound event. Option Explicit On Option Strict On Class MyPageClass Privat...

Entity Framework object limitations in aggregate LINQ query

I have a rather complicated query that I'd like to have return some particular types. Mostly concerning date / time calculations or string values, the Entity Framework seems to be spitting the dummy when I try anything further than a System.DateTime type: The specified type member 'Date' is not supported in LINQ to Entities. Onl...

Instantiating an anonymous type returns null

I'm having a problem instantiating an anonymous type in my code. For some reason, TResponse response = default(TResponse); returns null, even though TResponse has a constructor for it. Am I being dumb?! Class: public class MyClass { public MyResponse GetResponse(MyRequest request) { return Service<MyRequest, MyResponse>.MakeR...

Is there a way to create anonymous structs in C#?

There doesn't seem to be any way as anonymous types derive from object. But I thought I'd ask since much of the time we use anonymous types in simple query expressions to extract subsets of data to be used in those anonymous types we create. It just seems to me they should be structs (value types) for greater memory efficiency vs. refere...

Possible to bind an anonymous list<> to a gridview?

I'm creating an anonymous List<> here: var pip = new { MCP = "", Measure = "", Year = "", url1 = "", url2 = "", url3 = "" }; var PipList = (new[] { pip }).ToList(); The I loop through my code and load that list with items and bind it to my gridview: PipList.RemoveAt(0); gvReport.DataSource = PipList; gvReport.DataBind(); When I de...

Anonymous Types

I have a Dictionary(TKey, TValue) like Dictionary<int, ArrayList> Deduction_Employees = new Dictionary<int, ArrayList>(); and later I add to that array list an anonymous type like this var day_and_type = new { TheDay = myDay, EntranceOrExit = isEntranceDelay }; Deduction_Employees[Employee_ID].Add(day_and_type); Now h...

"==" Operator Doesn't Behave Like Compiler-generated Equals() override for anonymous type

According to the MSDN: Because the Equals and GetHashCode methods on anonymous types are defined in terms of the Equals and GetHashcode of the properties, two instances of the same anonymous type are equal only if all their properties are equal. However, the following code demonstrates the compiler generated implementatio...

Passing an instance of anonymous type over WCF

I have a WCF service method that expects an object and then retrieves its properties using reflection. On the client side I create an anonymous type object var obj = new {FirstName="John", LastName="Doe"} and pass it to the method. I'm getting an exception: Type '<>f__AnonymousType0`2[System.String,System.String]' cannot be serial...

Declaration of Anonymous types List

Is there any way to declare a list object of anonymous type. I mean List<var> someVariable = new List<var>(); someVariable.Add( new{Name="Krishna", Phones = new[] {"555-555-5555", "666-666-6666"}} ); This is because I need to create a collection at runtime. Thanks in advance ...

How to access anonymous object's property

I have a WinForms combobox to which I bind a list of anonymous objects (printer descriptions and locations). The goal here is a to select a default printer (which matches printer location). But within a foreach loop below, I am having trouble accessing the anonymous object's properties. I know of a work-around (I tried a private nes...

Assigning property of anonymous type via anonymous method

I am new in the functional side of C#, sorry if the question is lame. Given the following WRONG code: var jobSummaries = from job in jobs where ... select new { ID = job.ID, Description = job.Description, Fi...

Can I serialize Anonymous Types as xml?

I understood that anonymous types are marked private by the compiler and the properties are read-only. Is there a way to serialize them to xml (without deserialize) ? It works with JSON, how can I do it with XML? ...

Cannot iterate of a collection of Anonymous Types created from a LINQ Query in VB.NET

Ok everyone, I must be missing something here. Every LINQ example I have seen for VB.NET anonymous types claims I can do something like this: Dim Info As EnumerableRowCollection = pDataSet.Tables(0).AsEnumerable Dim Infos = From a In Info _ Select New With {.Prop1 = a("P...