我在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"
与
== 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知识可以将它们转移到我的特定需求。好吧!
答案 0 :(得分: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);
答案 1 :(得分: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);
然后p.s.w.g的答案应该有效,而你的问题在于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);