无法将匿名列表转换为LINQ中的已知列表 [英] Unable to cast anonymous list into known list in LINQ

查看:60
本文介绍了无法将匿名列表转换为LINQ中的已知列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ查询,它从一张表中返回结果.我需要将其转换为该表模型的列表. groupManager的匿名类型为List< a> where a为{Group g5}

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();

此代码似乎不起作用.我遇到的错误是{无法将类型'<> f__AnonymousType11`1 [Group]'的类型强制转换为类型'Group'."}我是否缺少某些内容?

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?

推荐答案

hmmm ...您尝试过吗?

hmmm... did you try this?

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

或这个

 select g5).ToList();

在不了解有关示例中的组对象或其他类型的情况下很难说更多的话.

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

这篇关于无法将匿名列表转换为LINQ中的已知列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆