0

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();

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

Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1> to System.Linq.IQueryable<AnonymousType#2>

What am I missing?

Jeppe Stig Nielsen
  • 54,796
  • 9
  • 96
  • 154
Khadim Ali
  • 2,406
  • 2
  • 26
  • 56

3 Answers3

1

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();

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 
}

to another:

new { 
    cust_id = g.Key.cust_id, 
    cust_code = g.Key.cust_code, 
    cust_name = g.Key.cust_name 
}

so a conversion error occurs.

Try this:

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(); 
Alex Filipovici
  • 29,732
  • 5
  • 50
  • 76
1

Change

_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

to

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(); 
I4V
  • 33,572
  • 3
  • 63
  • 78
0

When you apply the When clause you use the type

(cust, sa) => new { customer = cust, advice = sa }.

But after you change to

new { cust_id = g.Key.cust_id, cust_code = g.Key.cust_code, cust_name = g.Key.cust_name }

They are different types to compiler.

Alessandro D'Andria
  • 7,975
  • 2
  • 31
  • 30
  • It depends on what you want. Maybe apply the where just before ToList(). Something like this `.Where(e => e.cust_code == custcode)` I think could work. – Alessandro D'Andria Aug 15 '13 at 09:19
  • It didn't work. In fact, it still returns error after removing the conditional where altogether. However, I have to try other suggestions now. – Khadim Ali Aug 15 '13 at 10:42