LINQ to SQL error in .Join ()

I am trying to query a database and join two tables. I've never used Join () this way, and I get an error on the second Join ():

var adjustments = data.Inventory_ARCHIVEs
    .Where(i => i.Location == comboBox3.Text && 
        upcCodes.Contains(i.UPCCode) && 
        (i.AVtime.Value.Date >= dateTimePicker1.Value.Date && 
            i.AVtime.Value.Date <= dateTimePicker1.Value.AddDays(1).Date) && 
        (i.BVtime.Value.Date >= dateTimePicker1.Value.Date && 
            i.BVtime.Value.Date <= dateTimePicker1.Value.AddDays(1).Date))
    .GroupBy(i => new { i.UPCCode })
    .Select(i => new
    {
        ID = i.Max(x => x.ID),
        i.Key.UPCCode
    })
    .Join(data.Inventory_ARCHIVEs, a => a.ID, 
        b => b.ID, (a, b) => new { a, b })
    .Join(data.BQItems, x => new { x.a.UPCCode, x.b.Location }, 
        y => new { y.UPC_Code, y.Location }, (x, y) => new
    {
        ID = x.a.ID,
        UPCCode = x.a.UPCCode,
        Date = x.b.BVtime.Value.Date,
        Description = y.Description,
        BVamount = x.b.BVamount,
        AVamount = x.b.AVamount,
        Difference = x.b.AVamount - x.b.BVamount,
        AverageCost = x.b.AverageCost,
        ExtCost = (x.b.AVamount - x.b.BVamount) * x.b.AverageCost
    });

      

x.a.UPCCode

, x.b.Location

, y.UPC_Code

And y.Location

- lines.

This is mistake:

Type arguments for 'System.Linq.Enumerable.Join <TOuter, TInner, TKey, TResult> (System.Collections.Generic.IEnumerable <TOuter>, System.Collections.Generic.IEnumerable <TInner>, System. Func <TOuter, TKey>, System.Func <TInner, TKey>, System.Func <TOuter, TInner, TResult>) 'cannot be decommissioned. Try to specify type arguments explicitly.

If I DO NOT include the join on the Location column and just use "UPCCode" it works, but when I add the second join on the column I get the error

+3


source to share


2 answers


I suspect this is the problem - it is at least one problem:

.Join(data.BQItems, x => new { x.a.UPCCode, x.b.Location }, 
                    y => new { y.UPC_Code, y.Location },
                    ...)

      

You are trying to concatenate using two different anonymous types as key types. They have different properties: one has UPCCode

, the other has UPC_Code

. You probably want:



.Join(data.BQItems, x => new { x.a.UPCCode, x.b.Location },
                    y => new { UPCCode = y.UPC_Code, y.Location },
                    ...)

      

Or just combine with your property names to use everywhere UPCCode

or UPC_Code

rather than a mixture.

+13


source


You should care the most about the data type on both sides of the equals clause, they must be the same data type, like int and int, or strings and strings.



Or, using a lambda expression, the second and third parameters must be the same datatype in the Join clause.

+1


source







All Articles