Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 doesnt work, and from what little I know of LINQ it is because you cant 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!

share|improve this question
1  
Does it give you an error? or does it just return the wrong results? –  p.s.w.g Apr 25 '13 at 18:56
    
No results. However I know the result of getBranch() is correct. –  Phill Healey Apr 25 '13 at 18:58

2 Answers 2

up vote 2 down vote accepted

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);
share|improve this answer
    
This linq excluding the orderby bit were written for me by the guys who make the adx Portals for Dynamics eg adxstudio.com –  Phill Healey Apr 25 '13 at 19:09
1  
@PhillHealey I agree with Daryl, if var branch = "scotland" works but var branch = getBranch() doesn't, the error has to be in getBranch(). –  p.s.w.g Apr 25 '13 at 19:29
    
Yeah it looks like I wasted hours of my day on a simple capilisation of the first letter scenario! Damn this job! ;-) –  Phill Healey Apr 25 '13 at 19:41
    
If you started using the early-bound classes that would never happen. :) Btw, I encourage everyone if they aren't using early-bound classes to generate classes with strings for the field names: replacing CreateQuery("adx_eventtag_event") with CreateQuery(adx_eventtag_event.EntityLogicalName) and tag["adx_name"] with tag[adx_eventtag.fields.adx_name] - not as good as early bound but better than hard-coding the strings all over the code. –  Nicknow Apr 25 '13 at 21:31
    
@Nicknow How so? –  Phill Healey Apr 28 '13 at 12:09

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);
share|improve this answer
    
Really? That simple? Doh!!! Ill give it a try. –  Phill Healey Apr 25 '13 at 18:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.