1

我在 LINQ 中有以下语句:

var eventsWithTag = (from occurence in occurrences1
                     join eventTag in serviceContext.CreateQuery("adx_eventtag_event")
                         on occurence.Event.GetAttributeValue<Guid>("adx_eventid") equals eventTag.GetAttributeValue<Guid>("adx_eventid")
                     join tag in serviceContext.CreateQuery("adx_eventtag")
                         on eventTag.GetAttributeValue<Guid>("adx_eventtagid") equals tag.GetAttributeValue<Guid>("adx_eventtagid")
                     where tag.GetAttributeValue<string>("adx_name") == "scotland"
                     select occurence).OrderBy(x => x.Start);

在这种情况下,这适用于“Scotland”的固定字符串搜索。但是我需要替换它以反映当前页面主题。所以基本上我需要更换

== "scotland"

== getBranch()

其中 getBranch 将相关分支名称作为字符串返回。

这将导致我尝试

eventsWithTag = (from occurence in occurrences1
join eventTag in serviceContext.CreateQuery("adx_eventtag_event") on occurence.Event.GetAttributeValue<Guid>("adx_eventid") equals eventTag.GetAttributeValue<Guid>("adx_eventid")
join tag in serviceContext.CreateQuery("adx_eventtag") on eventTag.GetAttributeValue<Guid>("adx_eventtagid") equals tag.GetAttributeValue<Guid>("adx_eventtagid")
where tag.GetAttributeValue<string>("adx_name") == getBranch()
select occurence).OrderBy(x => x.Start);

但这不起作用,从我对 LINQ 知之甚少的情况来看,这是因为您不能以这种方式使用变量。

所以我的问题是:如何将上面的 LINQ 查询与分支的动态值一起使用。

请注意:我看过其他关于此的帖子,但我显然没有 LINQ 知识来将它们转移到我的特定需求。刚刚!

4

2 回答 2

2

一切看起来都正确,所以我会仔细检查您的 getBranch()...

如果这有效:

var branch = "scotland";
eventsWithTag = 
    (from occurence in occurrences1
     join eventTag in serviceContext.CreateQuery("adx_eventtag_event") on occurence.Event.GetAttributeValue<Guid>("adx_eventid") equals eventTag.GetAttributeValue<Guid>("adx_eventid")
     join tag in serviceContext.CreateQuery("adx_eventtag") on eventTag.GetAttributeValue<Guid>("adx_eventtagid") equals tag.GetAttributeValue<Guid>("adx_eventtagid")
     where tag.GetAttributeValue<string>("adx_name") == branch 
     select occurence)
    .OrderBy(x => x.Start);

然后 pswg 的答案应该有效,而您的问题在于 getBranch 返回的值与任何记录都不匹配...

在稍微不同的问题上,我对为什么需要在 equals 语句中获取实际属性感到有些困惑,因为根据this,它应该像这样正常工作:

var branch = "scotland";
eventsWithTag = 
    (from occurence in occurrences1
     join eventTag in serviceContext.CreateQuery("adx_eventtag_event") on occurence.Event["adx_eventid"] equals eventTag["adx_eventid"]
     join tag in serviceContext.CreateQuery("adx_eventtag") on eventTag["adx_eventtagid"] equals tag["adx_eventtagid"]
     where tag["adx_name"] == branch 
     select occurence)
    .OrderBy(x => x.Start);
于 2013-04-25T19:07:06.530 回答
2

您可以事先拨打电话:

var branch = getBranch();
eventsWithTag = 
    (from occurence in occurrences1
     join eventTag in serviceContext.CreateQuery("adx_eventtag_event") on occurence.Event.GetAttributeValue<Guid>("adx_eventid") equals eventTag.GetAttributeValue<Guid>("adx_eventid")
     join tag in serviceContext.CreateQuery("adx_eventtag") on eventTag.GetAttributeValue<Guid>("adx_eventtagid") equals tag.GetAttributeValue<Guid>("adx_eventtagid")
     where tag.GetAttributeValue<string>("adx_name") == branch 
     select occurence)
    .OrderBy(x => x.Start);
于 2013-04-25T18:41:26.510 回答