C# linqgroupby抛出错误
您好,我上面的查询连接了几个表,所有的表都正常工作。然后我意识到一些外键可能是空的 我研究了C# linqgroupby抛出错误,c#,linq,join,lambda,linq-to-entities,C#,Linq,Join,Lambda,Linq To Entities,您好,我上面的查询连接了几个表,所有的表都正常工作。然后我意识到一些外键可能是空的 我研究了GroupBy和DefaultIfEmpty的用法,它们听起来很有希望,因此我尝试将第一个Join更改为GroupJoin打开,但这引发了一个错误: var devSum = repository.Devices .Where(dev => dev.Id == deviceId) .Join(repository.Ma
GroupBy
和DefaultIfEmpty
的用法,它们听起来很有希望,因此我尝试将第一个Join
更改为GroupJoin
打开,但这引发了一个错误:
var devSum = repository.Devices
.Where(dev => dev.Id == deviceId)
.Join(repository.ManagementGroups, device => device.ManagementGroupId, mGroup => mGroup.Id, (device, mGroup) => new { device, mGroup.Name })
.Join(repository.DataGroups, device_mGroup => device_mGroup.device.DataGroupId, dGroup => dGroup.Id, (device_mGroup, dGroup) => new { device_mGroup.device, managerName = device_mGroup.Name, dataName = dGroup.Name })
.Join(repository.DeviceTypes, d => d.device.TypeId, t => t.Id, (d, t) => new { d.device, d.dataName, d.managerName, TypeName = t.Name })
.SingleOrDefault();
有人能帮忙吗?
mGroup
行中的(设备,mGroup)=>new{device,mGroup.Name}
实际上代表了整个集合。您需要对其执行选择:
var devSum = repository.Devices
.Where(dev => dev.Id == deviceId)
.GroupJoin(repository.ManagementGroups, device => device.ManagementGroupId, mGroup => mGroup.Id, (device, mGroup) => new { device, mGroup.Name })
.Join(repository.DataGroups, device_mGroup => device_mGroup.device.DataGroupId, dGroup => dGroup.Id, (device_mGroup, dGroup) => new { device_mGroup.device, managerName = device_mGroup.Name, dataName = dGroup.Name })
.Join(repository.DeviceTypes, d => d.device.TypeId, t => t.Id, (d, t) => new { d.device, d.dataName, d.managerName, TypeName = t.Name })
.SingleOrDefault();
'AnonymousType#1' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'AnonymousType#1' could be found (are you missing a using directive or an assembly reference?)
有关更多信息,请查看GroupJoin页面。能否显示引发异常的代码?以及GroupJoin
版本。。。where?添加了我编辑过的代码,错误出现在尝试获取mGroup.nameThankyu IronMan的行末尾的.GroupJoin行上,但这会引发错误17无效的匿名类型成员声明符。匿名类型成员必须使用成员分配、简单名称或成员访问权限声明。MSDN的示例中似乎有这样的声明。我已经相应地编辑了我的答案。谢谢,这至少消除了那一行的错误!我会继续努力改正其余的错误,然后回来接受我的答案。其他的错误只是在第二行改名字,就像第一行改名字一样。谢谢你的帮助,朋友。没问题。祝你在项目上好运。
(device, mGroup) => new { Device = device, NameGroup = mGroup.Select(m => m.Name) }