EF GROUPBY:不包含'BriefTitle',没有扩展方法的定义“BriefTitle”接受式的第一个参数 [英] EF GroupBy: does not contain a definition for 'BriefTitle' and no extension method 'BriefTitle' accepting a first argument of type
问题描述
什么是错此查询?
VAR模型=
SessionObjectsMSurvey.ContractList
。凡(Y =方式> y.ContractTitle.ToUpper()包含(上))
.GroupBy(G =>新建
{
g.BriefTitle,
g.ContractId
})
。选择(
X =>新建
{
标签= x.BriefTitle,
ID = x.ContractId.ToString()
})采取(20)。
SessionObjectsMSurvey.ContractList是IEnumerable集合。
这将无法编译,我得到;
错误13'System.Linq.IGrouping'
不包含定义BriefTitle',没有扩展方法
'BriefTitle接受式
'System.Linq.IGrouping'
可以找到的第一个参数(是否缺少using指令或程序集
参考?)
块引用>
解决方案我认为你需要插入
。重点
在你的。选择(...)
调用两个地方。结果在.GroupBy(...)
是IGrouping
的可查询和界面包含了。重点
属性让你访问组的关键。VAR模型=
SessionObjectsMSurvey.ContractList
。凡(Y =方式> y.ContractTitle.ToUpper()包含(上))
.GroupBy(G =>新建
{
g.BriefTitle,
g.ContractId
})
。选择(
X =>新建
{
标签= x.Key.BriefTitle //这里
ID = x.Key.ContractId.ToString()//这里
})以(20)。
What is wrong with this query?
var model = SessionObjectsMSurvey.ContractList .Where(y => y.ContractTitle.ToUpper().Contains(upper)) .GroupBy(g => new { g.BriefTitle, g.ContractId }) .Select( x => new { label = x.BriefTitle, id = x.ContractId.ToString() }).Take(20);
SessionObjectsMSurvey.ContractList is an IEnumerable collection.
This won't compile, I get;
Error 13 'System.Linq.IGrouping' does not contain a definition for 'BriefTitle' and no extension method 'BriefTitle' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)
解决方案I think you need to insert
.Key
at two places in your.Select(...)
call. The result of the.GroupBy(...)
is a queryable ofIGrouping
and that interface contains a.Key
property to let you access the key of your group.var model = SessionObjectsMSurvey.ContractList .Where(y => y.ContractTitle.ToUpper().Contains(upper)) .GroupBy(g => new { g.BriefTitle, g.ContractId }) .Select( x => new { label = x.Key.BriefTitle, // Here id = x.Key.ContractId.ToString() // And here }).Take(20);
这篇关于EF GROUPBY:不包含'BriefTitle',没有扩展方法的定义“BriefTitle”接受式的第一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!