开发者

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
开发者_JAVA百科                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() }


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


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)


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜