Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ query syntax to method syntax

I use asp.net, c# and EF4.

I'm puzzled by this LINQ query:

  var queryContents = from a in context.CmsContentsAssignedToes
                    where a.UserId == myUserGuid
                    join cnt in context.CmsContents
                    on a.ContentId equals cnt.ContentId
                    where cnt.TypeContent == myTypeContent & cnt.ModeContent == myModeContent
                    select cnt;

I would like write its equivalent in LINQ method syntax to retrieve CmsContents.

In my conceptual model are two entity types:

  • CmsContent
  • CmsContentsAssignedTo

and their entity sets:

  • CmsContents
  • CmsContentsAssignedToes

and navigation properties:

  • in CmsContent --> CmsContentsAssignedTo RETURN: --> Instance of CmsContentsAssignedTo
  • in CmsContentsAssignedTo --> CmsContent RETURN: --> Instance of CmsContent

Do you know how to do it? I tried for more that one day but I cannot solve it!

Thanks for your time!

like image 434
GibboK Avatar asked Dec 29 '22 00:12

GibboK


1 Answers

The equivalent method syntax is:

 var queryContents = context.CmsContentsAssignedToes
                            .Where(a => a.UserId == myUserGuid)
                            .Join(context.CmsContents,
                                  a => a.ContentId,
                                  cnt => cnt.ContentId,
                                  (a, cnt) = new { a, cnt })
                            .Where(z => z.cnt.TypeContent == myTypeContent &
                                        z.cnt.ModeContent == myModeContent)
                            .Select(z => z.cnt);

See my Edulinq blog post on query expressions for more details of how this works, and particularly where the "z" comes from (it's not really called "z"; it doesn't have a name as it's a transparent identifier).

like image 119
Jon Skeet Avatar answered Jan 18 '23 03:01

Jon Skeet