0

此查询正在编译且没有错误:

            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>

我错过了什么?

4

3 回答 3

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(); 
于 2013-08-15T10:29:07.217 回答
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();

在您的第一个场景中,只涉及一个匿名类型,因此_entityListList<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(); 
于 2013-08-15T09:04:22.863 回答
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 }

它们是编译器的不同类型。

于 2013-08-15T09:00:17.863 回答