JayX
JayX

Reputation: 1784

LINQ query complex join problem

So I was trying to convert a SQL into LINQ query

the logic is:

  JOIN SalesPeriod SP1 
  ON
     SP1.SalesPeriodId = SE1.SalesPeriodId AND SP1.SalePeriodId = .....(XML stuff)

but it keeps complaining the types on both sides of equals statement don't match

Any ideas?

Note: I declared b and d because it doesn't accept anonymous type members and I tested two equal conditions separately and they both work

thanks

   join SP1 in fentities.SalesPeriods
                on new { SE1.SalesPeriodId, b = XDocument.Load(MI.Body).Element("ns:Transfer").Element("ns:ReceivedBy").Element("ns:Id").FirstNode.ToString() }
                equals new { SP1.SalesPeriodId, d = SP1.SalesPeriodId.ToString() }

Upvotes: 0

Views: 606

Answers (3)

Kelqualyn
Kelqualyn

Reputation: 509

Don't stuck on putting complete join condition into 'ON' clause. Split condition on two parts, put one of them into 'ON' clause and another into 'WHERE' clause.

    join SP1 in fentities.SalesPeriods
            on SE1.SalesPeriodId equals SP1.SalesPeriodId        
    where XDocument.Load(MI.Body).Element("ns:Transfer").Element("ns:ReceivedBy").Element("ns:Id").FirstNode.ToString() == SP1.SalesPeriodId.ToString()

Upvotes: 0

Quintin Robinson
Quintin Robinson

Reputation: 82335

Your two anonymous types do not match, b & d to be specific.. try aligning the signatures..

join SP1 in fentities.SalesPeriods
            on new { SE1.SalesPeriodId, b = XDocument.Load(MI.Body).Element("ns:Transfer").Element("ns:ReceivedBy").Element("ns:Id").FirstNode.ToString() }
            equals new { SP1.SalesPeriodId, b = SP1.SalesPeriodId.ToString() }

In that example both anonymous objects will have the same property definitions (SalesPeriodId and b)

Upvotes: 1

Jeff Mercado
Jeff Mercado

Reputation: 134841

Simple, they're not the same (compatible) types. The first key has a SalesPeriodId of type whatever, and b of type string. The second key has a SalesPeriodId of type whatever (probably the same as the first's), and d of type string. You can't compare these to eachother. It must have the same properties of the same types declared in the same order. Just pick one of the names b or d and use that name.

...
join SP1 in fentities.SalesPeriods
    on new { SE1.SalesPeriodId, b = XDocument.Load(MI.Body).Element("ns:Transfer").Element("ns:ReceivedBy").Element("ns:Id").FirstNode.ToString() }
    equals new { SP1.SalesPeriodId, b = SP1.SalesPeriodId.ToString() }

Upvotes: 3

Related Questions