TheFallenOne
TheFallenOne

Reputation: 1686

Unable to cast anonymous list into known list in LINQ

I have a LINQ Query which returns me results from one table. I need to convert it into the List of that table model. The anonymous type of groupManager is List<a> wherea is {Group g5}

var groups = new List<Group>();
var groupManager = (from a in db.AUsers
                                join b in db.BUsers on a.Id equals b.UserID into group1
                                from g1 in group1.DefaultIfEmpty()
                                join c in db.UserRoles on g1.ID equals c.UserID into group2
                                from g2 in group2.DefaultIfEmpty()
                                join d in db.Roles on g2.RoleID equals d.ID into group3
                                from g3 in group3.DefaultIfEmpty()
                                join e in db.RoleGroups on g3.ID equals e.RoleID into group4
                                from g4 in group4.DefaultIfEmpty()
                                join f in db.Groups on g4.GroupID equals f.ID into group5
                                from g5 in group5.DefaultIfEmpty()
                                where a.Id == user.ID && g5.Name != ""
                                select new{ Group = g5}).ToList();

groups = groupManager.Cast<Group>().ToList();

This code does not seem to work.The error I am getting is {"Unable to cast object of type '<>f__AnonymousType11`1[Group]' to type 'Group'."} Am I missing something?

Upvotes: 0

Views: 695

Answers (4)

TheRock
TheRock

Reputation: 1541

If you select the actual object then that's what you get back as an IEnumerable of Group:

var groups = new List<Group>();
var groupManager = (from a in db.AUsers
                            join b in db.BUsers on a.Id equals b.UserID into group1
                            from g1 in group1.DefaultIfEmpty()
                            join c in db.UserRoles on g1.ID equals c.UserID into group2
                            from g2 in group2.DefaultIfEmpty()
                            join d in db.Roles on g2.RoleID equals d.ID into group3
                            from g3 in group3.DefaultIfEmpty()
                            join e in db.RoleGroups on g3.ID equals e.RoleID into group4
                            from g4 in group4.DefaultIfEmpty()
                            join f in db.Groups on g4.GroupID equals f.ID into group5
                            from g5 in group5.DefaultIfEmpty()
                            where a.Id == user.ID && g5.Name != ""
                            select g5).ToList()
groups = groupManager;

The .ToList() will then convert it to a list. No need to create a dynamic object and then cast.

Upvotes: 0

Paweł Hemperek
Paweł Hemperek

Reputation: 1180

Provided answers were absolutely correct, but I guess it could be good to point one subtle thing out regarding Cast method (and to answer "Am I missing something" part).

Cast method really serves different purpose than casting all objects in list to another type. No surprise that compiler throws this exception since what Cast does is taking IEnumerable (non generic version) and returning the same collection, but with generic IEnumerable<T>. This is one of two methods in LINQ that are taking non generic IEnumerable as an argument (the second is OfType), so you could have a collection of given type, cast it to IEnumerable<T> and use other LINQ methods which require IEnumerable<T> as an argument.

Just look at the source code

public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source) {
    IEnumerable<TResult> typedSource = source as IEnumerable<TResult>;
    if (typedSource != null) return typedSource;
    if (source == null) throw Error.ArgumentNull("source");
    return CastIterator<TResult>(source);
}

and in CastIterator

foreach (object obj in source) yield return (TResult)obj;

so your collection should be able to be casted to given type T in order to Cast to work. It won't cast your anonymous types to concrete types.

Upvotes: 0

Hogan
Hogan

Reputation: 70523

hmmm... did you try this?

 select new Group(g5)).ToList();

or this

 select g5).ToList();

hard to say more without knowing anything about the group object or the other types in your example.

Upvotes: 1

Derrick Moeller
Derrick Moeller

Reputation: 4950

You can likely do this without the anonymous type or the cast.

var groups = (from a in db.AUsers
              // Your query...
              select new Group
              {
                  // Properties of Group
                  Name = g5.Name,
                  AnotherProperty = g5.AnotherProperty
              }).ToList();

Upvotes: 0

Related Questions