1

Linq and EF4.

I have this Linq query in query syntax I would like convert into query method.

Are you able to do it? I tried more tha 2 hours without success :-(

Thanks for your time

CmsContent myContentObj = (from cnt in context.CmsContents
                   from categoy in cnt.CmsCategories
                   where categoy.CategoryId == myCurrentCategoryId && cnt.ContentId == myCurrentContentId
                   select cnt).Single();
2
  • 1
    It's spelled syntax - not "sintax" .... Commented Mar 15, 2011 at 12:44
  • thanks marc_s for your comment Commented Nov 29, 2012 at 8:54

3 Answers 3

2

My original answer selected the wrong item. It's a bit more complicated than what I had (which Ani has posted). Here's what I believe is an equivalent query however and should perform better:

CmsContent myContentObj =
    context.CmsContents
           .Where(cnt => cnt.ContentId == myCurrentId
                      && cnt.CmsCategories
                            .Any(categoy => categoy.CategoryId == myCurrentCategoryId))
           .Single();
Sign up to request clarification or add additional context in comments.

2 Comments

hi jeff does NOT work error Error 1 Cannot implicitly convert type 'CmsCategory' to 'CmsContent'
@GibboK: Ah oops, overlooked what was being selected. It's actually more like what Ani has.
1

Here is a non-direct translation that I believe performs the same task in much less code:

var myContentObj = context.CmsContents.Single(
                        x => x.ContentId == myCurrentContentId && 
                        x.CmsCategories.Any(y => y.CategoryId == myCurrentCategoryId)
                    );

1 Comment

Hi it is working great, also for me it is very readable! thanks
1

Here's how the C# compiler actually does it, with some help from .NET Reflector to verify:

var myContentObj = context
                   .CmsContents  
                   .SelectMany(cnt => cnt.CmsCategories,
                               (cnt, categoy) => new { cnt, categoy })
                   .Where(a => a.categoy.CategoryId == myCurrentCategoryId
                            && a.cnt.ContentId == myCurrentContentId)
                   .Select(a => a.cnt)
                   .Single();

Essentially, the 'nested' from clauses results in a SelectMany call with a transparent identifier (an anonymous-type instance holding the 'parent' cnt and the 'child' categoy). The Where filter is applied on the anonymous-type instance, and then we do another Select projection to get back the 'parent'. The Single call was always 'outside' the query expression of course, so it should be obvious how that fits in.

For more information, I suggest reading Jon Skeet's article How query expressions work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.