4

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!

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jon for you explanation and link. Have a nice day!
Hi, I posted a question, I would like you expert opinion if you have time. stackoverflow.com/questions/5260346/… thanks in advance for your help.

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.