开发者

Group join with multiple conditions and one of them being with a constant

Here is a snippet of my (VB) LINQ:

From event_evn In xmlEvents.Descendants("event_evn") _
Join genre_gnr In xmlGenre.Descendants("genre_gnr") On event_evn.Element("evn_gnr_id") Equals genre_gnr.Element("gnr_id").Value _
Group Join eventdata_eda In xmlEventData.Descendants("eventdata_eda") On _
eventdata_eda.Element("eda_evn_id").Value Equals event_evn.Element("evn_id").Value And _
eventdata_eda.Element("eda_dty_id").Value Equals "15" _
Into edaL开发者_JAVA技巧ist = Group _
From eventdata_eda In edaList.DefaultIfEmpty() _
Where _

I get an error in the last line with this message: "You must reference at least one range variable on both sides of the 'Equals' operator" How do I do a group join with multiple conditions, where one of these conditions is to comparing with a constant?

I found two places with the answer:

http://forums.asp.net/p/1209451/2127071.aspx#2127071

LINQ to SQL - Left Outer Join with multiple join conditions

However it is both in C# and I need it in VB.

Thanks in advance :)


I had a similiar issue recently and I was able to get around it by declaring a variable of an anonymous type within the LINQ query to hold all the values I wanted to join on, and to use that new temporary variable in the join conditions so your snippet would look something like below:

From event_evn In xmlEvents.Descendants("event_evn") _
Let JoinVals = New With { .EvnId = event_evn.Element("evn_id").Value, _
                          .Fifteen = "15" } _
Join genre_gnr In xmlGenre.Descendants("genre_gnr") On event_evn.Element("evn_gnr_id")     Equals genre_gnr.Element("gnr_id").Value _
Group Join eventdata_eda In xmlEventData.Descendants("eventdata_eda") On _
eventdata_eda.Element("eda_evn_id").Value Equals JoinVals.EvnId And _
eventdata_eda.Element("eda_dty_id").Value Equals JoinVals.Fifteen _
Into edaList = Group _
From eventdata_eda In edaList.DefaultIfEmpty() _
Where _


To left join on a constant value I added the the following:

...
Group Join j In <YourJoinTable> On <YourEqualsClauses> Into Group
From g In Group.Where(Function(f) f.ColumnName = "<constantValue>").DefaultIfEmpty()

Using LinqPad I checked and the expected SQL is generated.

...
LEFT OUTER JOIN <YourJoinTable> ON <YourEqualsClauses>
AND [<YourJoinTable>].[ColumnName] = '<constantValue>'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜