Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Lambda Join Error - cannot be inferred from the usage

Tags:

join

lambda

linq

I had troubles joining two DbSets and continued to receive the "cannot be inferred error". I struggled to find a solution so I thought I would share my simple answer. There are several great posts from Jon Skeet and others but most of the answers were over my head.

Here is the code that was causing me trouble:

using(var db = new SomeDataContext())
    {
    db.DemandData
        .Where(demand=> demand.ID == SearchID)
        .Join(db.CUST_ORDER_LINE,
            supply=> new { supply.LINE, supply.SALES_ORDER_ID },
            demand=> new { demand.LINE_NO, demand.CUST_ORDER_ID },
            (supply, demand) => new { custOrderLineReturn = demand })
        .Select(s => s.custOrderLineReturn )
        .ToList();
    }
like image 528
Pete Avatar asked Aug 20 '12 20:08

Pete


1 Answers

I have done this join so many times that I could not figure out why it would not work until I found a post from Justin Niessner here that says "The names of the properties in the anonymous types (as well as their types) must match exactly." That lead me to this code:

using(var db = new SomeDataContext())
    {
  return db.DemandData
        .Where(demand=> demand.ID == SearchID)
        .Join(db.CUST_ORDER_LINE,
            supply=> new { LINE_NO = supply.LINE, CUST_ORDER_ID = supply.SALES_ORDER_ID },
            demand=> new { demand.LINE_NO, demand.CUST_ORDER_ID },
            (supply, demand) => new { custOrderLineReturn = demand })
        .Select(s => s.custOrderLineReturn )
        .ToList();
    }

In the sixth line I added variables LINE_NO = and CUST_ORDER_ID = that matched the field names in line seven.

like image 50
Pete Avatar answered Nov 20 '22 18:11

Pete