Based upon a prior question I had Here. I wanted to join two collections together so that the merged data could be then displayed within a DataGrid of a Silverlight UI.
Using the Linq Zip function I was able to merge my two collections as follows
public void CombineAllCollections()
{
var combineAll = Persons.Zip(PhoneNumbers, (x, y) => new
{
FirstName = x.FirstName,
LastName = x.LastName,
Address = x.Address,
State = x.State,
Zip = x.Zip,
AreaCode = y.AreaCode,
PhoneNumber = y.Number
});
}
This seems to do just what I wanted. However the output is an Anonymous Type. How can I cast the combinedAll type to a collection (List, ObservableColl. IENum) that I can then pass to a view in my UI ( Bind to a datagrid) . The output should then display within the grid a column and value for each item ( as listed above seven columns).
Thanks in advance
-Cheers
Instead of making an anonymous type, you could create a concrete type specifically for the merged results with all of the properties you specified on the anonymous type.
For example:
Where
PersonWithPhoneNumberis the type with all of the specified properties.Then you can call ToList() on the result to convert it to an
IList<PersonWithPhoneNumber>or you can leave it in the form it is in as anIEnumerable<PersonWithPhoneNumber>EDIT
Given the information that you provided, it would appear about the only effective way to store so many values while maintaining the ability to use LINQ and not have to define a separate type would be to create a dictionary for each item in the zipped collection, for example:
This way, you can have as many Key/Value pairs in the dictionary for each item as you would like. However, you essentially lose any type safety using this method and will end up boxing all value types for each property. This means you will have to know the type to cast back to. I would honestly not recommend doing this. I would simply just make a type, even if that type needs 100 properties. In this scenario auto-properties and a good text editor are your best friend.
You could also us the new dynamic type in C# 4 with the .NET 4.0 framework as in the following example:
This will introduce dynamic into your code, though. Which may not have the performance metrics you desire and can lead to a lot of run-time errors if you don’t keep an eye out on what you’re doing. This is because you could misspell a property while creating or accessing it (this is also an issue with the dictionary setup) which will lead to a run-time exception when you attempt to access it. Again, probably best to just use a concrete, named typed in the end.