具有多个条件的组连接,其中一个条件是常数

发布于 2024-08-19 18:37:55 字数 1000 浏览 5 评论 0原文

这是我的 (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 edaList = Group _
From eventdata_eda In edaList.DefaultIfEmpty() _
Where _

我在最后一行收到一条错误消息: “您必须在‘等于’运算符两侧至少引用一个范围变量” 如何进行具有多个条件的组连接,其中其中一个条件是与常量进行比较?

我找到了两个有答案的地方:
http://forums.asp.net/p/1209451/2127071.aspx# 2127071
LINQ to SQL - 具有多个连接条件的左外连接< /a>

但是它是 C# 语言的,我需要 VB 语言的。

提前致谢 :)

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 edaList = 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弱骨蛰伏 2024-08-26 18:37:55

我最近遇到了一个类似的问题,我可以通过在 LINQ 查询中声明一个匿名类型的变量来保存我想要连接的所有值,并在连接条件中使用该新的临时变量来解决这个问题,这样您的代码片段如下所示:

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 _

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 _
暮年慕年 2024-08-26 18:37:55

为了左连接常量值,我添加了以下内容:

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

使用 LinqPad 我检查并生成了预期的 SQL。

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

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>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文