anonymous-types

Set linq return property to null

How come setting OrganizationName = null does not work? I am doing a union and would like to set the value to null. from a in Activities select new { a.ActivityID, a.ActivityName, a.OrganizationID, OrganizationName = null } ...

C#: Compiler optimizations of anonymous types

OK OK, I know this is a hack, but this was for a tiny data-manipulation project and I wanted to play around. ;-) I was always under the impression that the compiler would examine all anonymous types used in a C# program and if the properties were the same, it would only create one class behind the scenes. So let's say I want to create ...

Can this Linq query be typed as anything other than "var"?

When I do a query that returns an anonymous type var assets = from Product p in Session.CreateLinq<Product>() where bundles.Contains(p.ProductBundle) select new {p.Asset, p.Asset.PropertyTbl}; Can I type the return to anything other than var? ...

How Iterate from list of Anonymous Types?

Hi, i have this linq to entities query: c.CreateQuery<T_Hotels>("T_Hotels").Where(item => item.Id > 0).GroupBy(item => new { item.Location, item.Name }).Select(group => new { group.Key, Total = group.Sum(item => item.Id) }) I want to implement everything in a method of a class Helper, how declare the expressions of GroupBy and Select...

Anonymous types query or normal query in LINQ

var emps = from x in DB where x.ID = 100 select x; var emp1 = from x1 in DB where x1.ID = 100 select new { x }; What is difference between these two queries. If we use anonymous-types is the performance will be increased or any other differences also there? ...

Create hierarchical anonymous type

Is there any way to create anonymous type that references instances of itself? var root = new { Name = "Root", Parent = ??? }; var child = new { Name = "Child", Parent = root }; var childOfChild = new { Name = "Grand child", Parent = child }; For example, we can reference delegate from itself: Action run = null; run = () => run(); ...

How can I retrieve strongly typed properties from an anonymous type?

I'm using anonymous types to pass collections of typed objects to a TemplateResolver, where the named placeholders in a newly instantiated text template may source values from multiple objects, e.g. var body = TemplateResolver.ResolveTemplate(template.ExternalRecipientBody, new {Sender = customer, NewJobCard = jobCard}); where the te...

Is there a way to specify an anonymous empty enumerable type?

I'm returning a Json'ed annonymous type: IList<MyClass> listOfStuff = GetListOfStuff(); return Json( new { stuff = listOfStuff } ); In certain cases, I know that listOfStuff will be empty. So I don't want the overhead of calling GetListOfStuff() (which makes a database call). So in this case I'm writing: return Json( ...

Creating a Stack<T> with anonymous type

Hi people, I have the a class Foo like this: class Foo { public int id{get;set;} public IEnumerable<Foo> Childs; //some other properties } Now I want to process some business logic on a Foo-Object and all it's children like this: public void DoSomeWorkWith(Foo x) { var firstItem = new {level = 0, item = x}; var s ...

Passing anonymous type as method parameters

In my plugin architecture I am currently passing a plugin name (string), method name (string) and parameters (object array) to my plugin service to execute the specified method and return the result (of type T). The plugin service's execute method can be seen below: public TResult Execute<TResult>(string pluginName, string operation, p...

Defining a lambda expression with an anonymous type contained within that lambda.

Hi, I'm trying to avoid a dynamic type in my lambda expression for grouping a collection. The type is defined anonymously at compile time (and unambiguously as far as I can tell). I'd rather not define the type as a full-fledged class as I'm only using it a few times in this single method. Sample code: Func<MyData, dynamic> dataGroupi...

Anonymous classes, temporary data, and collections of anonymous classes

I'm new to anonymous classes, and today I think I ran into the first case where I felt like I could really use them. I'm writing a method that would benefit from storing temporary data inside of a class, and since that class doesn't have any meaning outside of that method, using an anonymous class sure made sense to me (at least at the ...

VB.NET: Which As clause to use with anonymous type with Option Strict On?

Consider the requirement to always declare Option Strict On. We'll always need to declare variables with the As keyword. What would be the type of an anonymous type? Example : Dim product As ... = New With { Key .Name = "paperclips", .Price = 1.29 } What will follow the As? ...

Problem with LINQ, anonymous types, and closures

I have a piece of code that filters a list using LINQ, creates a list of instances of an anonymous type, and assigns an event handler to each instance: // Select every linear expression and create a menu item from it var items = from expr in expressionList.Expressions where expr.Type == ExpressionType.Linear let ...

Anonymous type for nested repeater (ASP .NET)

I have two nested repeaters in my *.aspx page. <asp:Repeater runat="server" id="rptMain"> <ItemTemplate> <h1><%#DataBinder.Eval(Container.DataItem, "Name")%></h1> <asp:Repeater runat="server" DataSource='<%# getUser(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "FieldKey"))) %>'> <HeaderTemplate><ol>...

Constructing generic lists of anonymous types and nested loops.

I wish to construct a list of anonymous types constructed by iterating through two other lists in a nested loop. var myList = new List<???>(); foreach (object x in GetAllX()) { if (Process(x)) { foreach (object y in GetAllY(x)) { myList.Add(new { X = x, Y = y ...

In c# convert anonymous type into key/value array?

I have the following anonymous type: new {data1 = "test1", data2 = "sam", data3 = "bob"} I need a method that will take this in, and output key value pairs in an array or dictionary. My goal is to use this as post data in an HttpRequest so i will eventually concatenate in into the following string: "data1=test1&data2=sam&data3=bob"...

Is there any way to xml serialize an anonymous type in .net using C#

Is there any way to serialize an anonymous type in .net? Normal XmlSerializer fails because the type has no parameterless constructor defined; and NetDataContractSeralizer fails becuase we can't tag DataContract or Serializable attribute to anonymous class. So is there any clever way around or we just can't do it? ...

Create a union of the members of anonymous types.

How could it be possible to union the members of instances of two or more anonymous (or maybe not anonymous) types, to create a resulting type instance with all the members? Pseudocode example: var anon1 = new {FirstName = "Bill"}; var anon2 = new {Surname = "Hicks"}; var anon3 = anon1.Union(anon2); Debug.WriteLine(anon3.FirstName + "...

Convert anonymous type to strong type for grouping query?

I've pieced together some information from other posts but I'm stuck. The first part works fine. Basically I query the database using LINQ and then I loop through the results generating a report. Dim results As System.Linq.IQueryable(Of Bud900Reports.tblBud_CONNECT) If options.Type = reportType.Organization Then results = From...