How can I convert anonymous type to strong type in LINQ?

I see nobody has addressed your need to convert an anonymous type to a named type explicitly, so here goes... By using select new { } you are creating an anonymous type, but you don't need to. You can write your query like this: List orders = (from item in (e. Argument as ListViewItem).AsParallel() select (SalesOrderMaster)item.

Tag) .Distinct() .ToList() Notice that the query selects (SalesOrderMaster)item. Tag without new { } so it doesn't create an anonymous type. Also note I added ToList() since you want a ListHowever, I agree with Mark and Guffa that using a parallel query here isn't you best option.

To use HashSetTag; HashSet orders = new HashSet(query) (I avoided using var so the returned types are clear in the examples. ).

I see nobody has addressed your need to convert an anonymous type to a named type explicitly, so here goes... By using "select new { }" you are creating an anonymous type, but you don't need to. You can write your query like this: List orders = (from item in (e. Argument as ListViewItem).AsParallel() select (SalesOrderMaster)item.

Tag) .Distinct() .ToList(); Notice that the query selects (SalesOrderMaster)item. Tag without new { }, so it doesn't create an anonymous type. Also note I added ToList() since you want a List.

This solves your anonymous type problem. However, I agree with Mark and Guffa that using a parallel query here isn't you best option. To use HashSet as Guffa suggested, you can do this: IEnumerable query = from item in (ListViewItem)e.

Argument select (SalesOrderMaster)item. Tag; HashSet orders = new HashSet(query); (I avoided using var so the returned types are clear in the examples. ).

The part in that code that is expensive is calling the Contains method on the list. As it's an O(n) operation it gets slower the more objects you add to the list. Just use a HashSet for the objects instead of a List.

The Contains method of the HashSet is an O(1) operation, so your loop will be an O(n) operation instead of an O(n*n) operation.

1 But adding items to a hash set is an O(n) operation every O(log n) adds, so you get O(n*log n) – configurator Apr 29 '09 at 13:22 Yes, adding items to a hash set is an O(n) operation sometimes, but so is adding items to a list, so the relative result is about the same. – Guffa Apr 29 '09 at 13:42 So to be clear, adding the item takes about the same time for both, it's determining whether it already contains the item which is much faster in HashSet. Is that it?

– Lucas Apr 29 '09 at 14:34.

Like Marc Gravell said, you shouldn't access the Tag property from different threads, and the cast is quite cheap, so you have: var items = (e. Argument as ListViewItem). Select(x=>x.

Tag) .OfType().ToList(); but then, you want to find distinct items - here you can try using AsParallel: var orders = items.AsParallel().Distinct().

This is a simple solution: codeproject.com/KBecause s/AnonymousTypeTransform.aspx.

Dim results As System.Linq. Type = reportType. Results = From p In db.

TableOldName = "tblFY" & options.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions