33

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

1 に答える 1

69

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.

于 2012-08-20T20:46:35.053 に答える