不能隐式转换类型'System.Collections.Generic.List< AnonymousType#1>'到'System.Linq.IQueryable< AnonymousType#2>' [英] Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Linq.IQueryable<AnonymousType#2>'
问题描述
正在编译此查询,而不会出现错误:
This query is being compiled without errors:
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子句返回编译时类型转换错误:
While adding a conditional where clause to the above query returns compile time type conversion error:
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>
to System.Linq.IQueryable< AnonymousType#2>
Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1>
to System.Linq.IQueryable<AnonymousType#2>
我缺少什么?
推荐答案
请考虑以下简化代码示例:
Consider the following simplified code sample:
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
是列表< AnonymousType#1>
。在第二种情况下,您可以从一个匿名类型更改返回的类型:
In your first scenario, there is only one anonymous type involved, hence _entityList
is a List<AnonymousType#1>
. In your 2nd scenario, you change the returned type from one anonymous type:
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();
这篇关于不能隐式转换类型'System.Collections.Generic.List< AnonymousType#1>'到'System.Linq.IQueryable< AnonymousType#2>'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!