ColdFIltered (3) [Avatar] Offline
#1
Trying to make a List<> of anonymous query types
Hi,

I'm looking to make a generic list of the anonymous type returned from a Linq expression, so I can loop over the results and add particular ones to a list. e.g.

var comments = from c in db.comments where .... select new { name= ..., By = (from ...) }

var commentsToFilter = List<?>; // Not sure how to reference the anon type!

If I use 'select c' as the return type of the query I can use:

var commentsToFilter = List<Foo.Comment>

How do I make a generic list if the return type is anonymous?

Just getting a hang of this Linq stuff - so far so good - but this has stumped me smilie
fabrice.marguerie (224) [Avatar] Offline
#2
Re: Trying to make a List&lt;&gt; of anonymous query types
If instead of looping over the results you use a LINQ query, you can simply use ToList.
For example:

var commentsToFilter = (from comment in comments where ... select comment).ToList();
or
var commentsToFilter = comments.Where(comment => ...lambda...).ToList();

If you really need to create a list with your anonymous type, you use the following method:

public static List<T> CreateEmptyList<T>(IEnumerable<T> source)
{
return new List<T>();
}


You can then write the following code:

var list = CreateEmptyList(comments);
foreach (var comment in comments)
{
if (comment.Length > 20)
list.Add(comment);
}
ColdFIltered (3) [Avatar] Offline
#3
Re: Trying to make a List&lt;&gt; of anonymous query types
I can't seem to make a list of lists with this technique e.g.

when I return 'select c' I can use:

var branches = new List<List<Foo.Comment>>();

with the anonymous return type I tried:

var branch = CreateEmptyList(comments);
var branches = CreateEmptyList(branch);
...
branch.Add(comment);
branches.Add(branch); // I get a "Cannot convert List<'a> to 'a" compile time error

Thanks for your time.
fabrice.marguerie (224) [Avatar] Offline
#4
Re: Trying to make a List&lt;&gt; of anonymous query types
You get this error because branch is a list of "Comment" (your anonymous type). When you use CreateEmptyList with branch as a parameter, it creates a new list of "Comment". What you really want is a list of list of "Comment".

All you have to do in your case is to add a new overload of CreateEmptyList that looks like this:

public static List<T> CreateEmptyList<T>(T element)
{
return new List<T>();
}


Out of curiosity, what are you doing in your loops that you can't do with LINQ? Could you show us a bit of your code, so we can see if it could be written with LINQ?
ColdFIltered (3) [Avatar] Offline
#5
Re: Trying to make a List&lt;&gt; of anonymous query types
Hi Fabrice,

Thanks for your patence with this.

I tried the additional CreateEmptyList method you suggested and it worked:

var branch = CreateEmptyList(comments);

I get intellisense indicating the returned type is List<List<'a>> which is what I wanted it to do.

As for a little bit of code - I'm really just feeling out Linq to see if certain things are possible, I don't have any 'purpose' for this List<'a> yet, but it will come in handy knowing I can make generic lists (and lists of lists) from an anonymous type returned from Linq expressions.

Thank you once again for your insight.