Reputation: 3180
I have the following statement in 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);
This works fine with the fixed string search of "Scotland" in this instance. However I need to replace that to reflect the current page topic. So essentially I need to replace:
== "scotland"
with:
== getBranch()
where getBranch returns the relevant branch name as a string.
This would lead me to try:
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);
But this doesn't work, and from what little I know of LINQ it is because you can't use variables in this way.
So my question is: How can I use the LINQ query above with a dynamic value for the branch.
Please note: I have seen other posts about this but I evidently don't have the LINQ knowledge to transfer them to my specific needs. Just yet!
Upvotes: 1
Views: 1101
Reputation: 18895
Everything looks right so I'd double check your getBranch()...
if this works:
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);
Then p.s.w.g's answer should work, and your issue lies with getBranch returning a value that doesn't match any records...
On a slightly different matter, I'm slightly confused as to why you need to get the actual attributes in your equals statement, because according to this, it should work fine like 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);
Upvotes: 2
Reputation: 149020
You can just make the call beforehand:
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);
Upvotes: 2