How to convert a LINQ query from query syntax to query method?

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) ).

Also for me it is very readable! Thanks – GibboK Mar 15 at 13:59.

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().

Jeff does NOT work error Error 1 Cannot implicitly convert type 'CmsCategory' to 'CmsContent' – GibboK Mar 15 at 12:52 This isn't correct at all. – Ani Mar 15 at 12:55 @GibboK: Ah oops, overlooked what was being selected. It's actually more like what Ani has.

– Jeff Mercado Mar 15 at 12:56.

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.

Var logQuery = from entry in xDoc. Revision = entry. Author = entry.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions