我在 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”的固定字符串搜索配合得很好。但是我需要替换它以反射(reflect)当前页面主题。所以基本上我需要更换:
== "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 知识来将它们转移到我的特定需求。就在这之前!
请您参考如下方法:
一切正常,所以我会仔细检查您的 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);
