将Linq查询表达式转换为等效的方法语法

时间:2011-07-19 09:55:48

标签: linq entity-framework

我使用LINQ,C#,EF4。

我在Linq中有这个查询表达式。我需要转换为方法语法中的等价物,但我对结构有一些疑问。你能给我一个很好的例子吗?谢谢你的帮助。

var myContentsForAuthor = from c in context.CmsContents
                          join a in context.CmsAuthors on c.AuthorId equals a.AuthorId
                          join u in context.aspnet_Users on a.UserId equals u.UserId
                          orderby c.Title ascending 
                          where u.UserId == myUserGuid && c.IsDeleted == false && c.Title.Contains(nameSearchString)
                          select c;

1 个答案:

答案 0 :(得分:2)

嗯,由于透明的标识符,这变得很复杂,但是类似于:

var myContentsForAuthor = context.CmsContents
                                 .Join(context.CmsAuthors,
                                       c => c.AuthorId
                                       a => a.AuthorId,
                                       (c, a) => new { c, a })
                                 .Join(context.aspnet_Users,
                                       z => z.a.UserId,
                                       u => u.UserId,
                                       (z, u) => new { z, u })
                                 .OrderBy(zz => zz.z.c.Title)
                                 .Where(zz => zz.u.UserId == myUserGuid &&
                                              zz.z.c.IsDeleted == false &&
                                              zz.z.c.Title.Contains(nameSearch))
                                 .Select(zz => zz.z.c);