anonymous-types

LINQ Group By to project into a non-anonymous type?

Hi, I have the following LINQ example: var colorDistribution = from product in ctx.Products group product by product.Color into productColors select new { Color = productColors.Key, Count = productColors.Count() }; All this works and makes perfect sense. What I'm trying to achieve is...

How to deal with duplicate anonymous type member conflicts?

I am creating an anonymous type and I have conflicting field names. The following code is failing because i.Name and i.Target.Name both have a property with the same name; "Name". How do I get around this ? Here is the code: i => new { i.Name, i.Target.Name, i.EndDate, i.LastUpdated }; ...

What wrong I am doing in IEnumerable(C#3.0)

If I write var v = (from r in stock.ReplacementLog select new { AssetId = stock.AssetId, Date = stock.ReferDate, ...

Return/consume dynamic anonymous type across assembly boundaries

The code below works great. If the Get and Use methods are in different assemblies, the code fails with a RuntimeBinderException. This is because the .Net runtime system only guarantees commonality of anonymous types (<string, int> in this case) within assemblies. Is there any way to fool the runtime system to overcome this? I can expe...

.NET how to output csv from enumeration of anonymous type?

Using FileHelpers, I decorated a class with [DelimitedRecord(",")] and was going to output an enumeration of objects of that type as CSV. But, it didn't work because my class inherits from ActiveRecordLinqBase<T>, which caused some problems. So, I was wondering if I could just select an enumeration of anonymous types and somehow have f...

Tuples vs. Anonymous Types vs. Expando object. (in regards to LINQ queries)

I am a beginner who finally started understanding anonymous types. (see old post http://stackoverflow.com/questions/3010147/what-is-the-return-type-for-a-anonymous-linq-query-select-what-is-the-best-way-t) So in LINQ queries you form the type of return value you want within the linq query right? It seems the way to do this is anonymou...

How can be possible to use anonymous type fields without know them ?

Hello everybody It is really looks so cool to me how GridView's DataSource property gets anonymous type and shows results in Grid . Simply Grid.DataSource = from order in db.OrdersSet select new { order.PersonSet.Name,order.PersonSet.SurName}; For example how can i write a propery or method which takes anonymous type and wr...

How to find out if object is of Anonymous type?

Possible Duplicate: Anonymous Types - Are there any distingushing characteristics? Can't find suitable property. if(new {a = 2, b= "z"}.GetType()...) what to put instead of ...? ...

is it possible to have a conditional field in an anonymous type

i have some code that looks like this and creates a list from an existing collection var items = items.ConvertAll(r => new { description = FormatDescription(r), start = r.Milestone.HasValue ? r.Milestone.Value.ToString("yyyy-MM-ddTHH:mm:ssZ") : DateTime.Today.ToString("yyyy-MM-ddTHH:mm:ssZ"),...

How to tell that a reflected type is anonymous

Possible Duplicates: How to identify anonymous methods in System.Reflection Anonymous Types - Are there any distingushing characteristics? The CLR doesn't know an anonymous type from a 'normal' type - that's something the compiler handles. How am I supposed to tell that a type found during reflection is anonymous? public st...

Using anonymous generic types with multiple generics

After reviewing this blog by Kirill Osenkov (How to create a generic List of anonymous types?) I am trying to do something a little more advanced and having issues. The following code compiles: var anon = new { One = "1", Two = "2" }; var result = DoSomething(anon); public static T DoSomething<T>(T value) { ret...

What's the equivalent VB.NET syntax for anonymous types in a LINQ statement?

I'm trying to translate some C# LINQ code into VB.NET and am stuck on how to declare an anonymous type in VB.NET. .Select(ci => new { CartItem = ci, Discount = DiscountItems.FirstOrDefault(di => di.SKU == ci.SKU) }) How do you translate C#'s new { ... } syntax into VB.NET? ...

How to return Anonymous Type while using Linq

Do any one know how to return an anonymous type. I am using Linq where i need to return the following code private <What's the return type to be provided here> SampleLinq(Int32 Num) { var query = (from dept in obj.DeptTable where dept.Id == Num select new { dept.DeptName, dept.DeptId }); return (query) }...

How do I combine multiple linq queries into one results set?

I have a multi-select checkbox. Depending on which one is checked, I want to combine the results into a single query. Sort of like: if (Checkbox1.Checked) { var query1 = from t in table1 ... } if (Checkbox2.Checked) { var query2 = from t in table2 ... } DataGridView1.DataSource = query1.Union(query2); // obviously doesnt ...

Linq to Objects and Anonymous Types

I'm new to Linq to Objects, and I've just hit the problem of anonymous types and scope. For example, this works just fine: Public Sub CreateResults() results = From e In CustomerList Join f In OrderList On CustomerList.ID Equals OrderList.ID Select New With {e.FirstName, e.LastName, f.Ord...

Why can't I pass an anonymous type as a parameter to a function?

I was trying to do something like below but it doesn't work. Why won't .NET let me do this? private void MyFunction(var items) { //whatever } ...

Why does Enum.GetValues() return names when using "var"?

Can anyone explain this? using System; namespace TestEnum2342394834 { class Program { static void Main(string[] args) { //with "var" foreach (var value in Enum.GetValues(typeof(ReportStatus))) { Console.WriteLine(value); } //with "int...

how can you loop through all fields of an anonymous type.

i want to take a list of anonymous types and export then to excel. I have the code to export to excel if i have the data, but i want to be able write some generic code to: Loop through all of the fields in the anonymous type so i can export it to excel. I would like to keep the ordering if possible as well. ...

How do you use a linq to sql join two tables and return the result?

I have been looking on Stack overflow but none of the answers seem to fully sort out this problem. The error I'm getting is: Cannot initialize type 'mvcTest.Models.MakeModelSpec' with a collection initializer because it does not implement 'System.Collections.IEnumerable' I have created a new type like this to get over the anonymous typ...

Playing with anonymous types

From Jon Skeet's wonderful book C# In Depth, First Edition: class Film { public string Name { get; set; } public int Year { get; set; } public override string ToString() { return string.Format("Name={0}, Year={1}", Name, Year); } } var films = new List<Film> { new Film {Name="Jaws", Year=1975}, new ...