正在编译此查询而没有错误:
var _entityList = context.customer
.Join(context.applications,
cust => cust.cust_id,
app => app.cust_id,
(cust, app) => new { customer = cust, application = app })
.Join(context.advices,
cust => cust.application.app_id,
sa => sa.app_id,
(cust, sa) => new { customer = cust, advice = sa })
.GroupBy(g => new { g.customer.customer.cust_id, g.customer.customer.cust_code, g.customer.customer.cust_name })
.Select(g => new { cust_id = g.Key.cust_id, cust_code = g.Key.cust_code, cust_name = g.Key.cust_name })
.ToList();
在上面的查询中添加条件where子句会返回编译时类型转换错误:
var _entityList = context.customer
.Join(context.applications,
cust => cust.cust_id,
app => app.cust_id,
(cust, app) => new { customer = cust, application = app })
.Join(context.advices,
cust => cust.application.app_id,
sa => sa.app_id,
(cust, sa) => new { customer = cust, advice = sa });
if (custcode != null && custcode != "")
_entityList = _entityList.Where(e => e.customer.customer.cust_code == custcode);
_entityList = _entityList
.GroupBy(g => new { g.customer.customer.cust_id, g.customer.customer.cust_code, g.customer.customer.cust_name })
.Select(g => new { cust_id = g.Key.cust_id, cust_code = g.Key.cust_code, cust_name = g.Key.cust_name })
.ToList(); // error on this line
无法隐式将System.Collections.Generic.List<AnonymousType#1>
类型转换为System.Linq.IQueryable<AnonymousType#2>
我错过了什么?
答案 0 :(得分:1)
考虑以下简化代码示例:
var _entityList = Enumerable.Range(0, 1)
.Select(i=>new {i1 =i, i2 = i+1});
_entityList = _entityList
//.Select(i => new { i1 = i.i1, i2 = i.i2 }) // works
//.Select(i => i) // works
.Select(i => new { i }) // fails
.ToList();
在您的第一个场景中,只涉及一个匿名类型,因此_entityList
是List<AnonymousType#1>
。在第二个场景中,您将从一个匿名类型更改返回的类型:
new {
customer = cust,
advice = sa
}
到另一个人:
new {
cust_id = g.Key.cust_id,
cust_code = g.Key.cust_code,
cust_name = g.Key.cust_name
}
因此发生转换错误。
试试这个:
var _entityList = context.customer
.Join(context.applications,
cust => cust.cust_id,
app => app.cust_id,
(cust, app) => new { customer = cust, application = app })
.Join(context.advices,
cust => cust.application.app_id,
sa => sa.app_id,
(cust, sa) => new { customer = cust, advice = sa })
.Where(e => (custcode != null && custcode != "")
? e.customer.customer.cust_code == custcode : true)
.GroupBy(g => new {
g.customer.customer.cust_id,
g.customer.customer.cust_code,
g.customer.customer.cust_name })
.Select(g => new {
cust_id = g.Key.cust_id,
cust_code = g.Key.cust_code,
cust_name = g.Key.cust_name })
.ToList();
答案 1 :(得分:1)
更改
_entityList = _entityList
.GroupBy(g => new { g.customer.customer.cust_id, g.customer.customer.cust_code, g.customer.customer.cust_name })
.Select(g => new { cust_id = g.Key.cust_id, cust_code = g.Key.cust_code, cust_name = g.Key.cust_name })
.ToList(); // error on this line
到
var result = _entityList
.GroupBy(g => new { g.customer.customer.cust_id, g.customer.customer.cust_code, g.customer.customer.cust_name })
.Select(g => new { cust_id = g.Key.cust_id, cust_code = g.Key.cust_code, cust_name = g.Key.cust_name })
.ToList();
答案 2 :(得分:0)
当您应用When子句时,使用类型
(cust, sa) => new { customer = cust, advice = sa }.
但是在你改成
之后new { cust_id = g.Key.cust_id, cust_code = g.Key.cust_code, cust_name = g.Key.cust_name }
它们是编译器的不同类型。