猿问

这个左外连接查询的正确 Linq 表达式是什么?

我无法将这个他的 sql 查询转换为一个 linq 表达式,该表达式将结果作为类的 IEnumerable 返回。


这是查询:


Select * from Posts left outer join Ownergroups on 

Posts.PostId=Ownergroups.PostID

Where Ownergroups.OwnerName = 'Group A' AND PostType = 'news'

这是唯一不抛出错误的表达式,但它也只返回一个结果。


        NewsViewModel vm = new NewsViewModel();


       vm.NewsItems =  (from an in db.Posts.Where(g => g.PostType == "News")

       from og in an.OwnerGroups.Where(g => g.OwnerName == "Group A")

        select an).Distinct().OrderByDescending(bb 

        =>bb.PostDate).ToList();

如果我尝试投影到一个新的选择,我会收到一个错误。当我尝试按 PostId 分组时,我得到了正确的结果,但无法将结果附加到我的 ViewModel;我收到一条错误消息“无法将类型 system.collections.generic 列表转换为 systems.collections.IEnumerable”


我真的很感激建议。


根据要求添加类:


     public class Post

{

    public int PostId { get; set; }


    public string PostType { get; set; }


    [Display(Name = "Top Title")]

    [MaxLength(300)]

    public string Headline1 { get; set; }


    [Display(Name = "Subtitle")]

    [MaxLength(300)]

    public string Headline2 { get; set; }


    public string Headline3 { get; set; }


    [Display(Name = "By Organization or Person")]

    [MaxLength(250)]

    public string Byline { get; set; }


    [Display(Name = "Text For Your Post")]

    [MaxLength(4999)]

    [AllowHtml]

    public string PostText1 { get; set; }


    [Display(Name = "Additional Text")]

    [MaxLength(4999)]

    [AllowHtml]

    public string PostText2 { get; set; }


    public string AuthorGroup { get; set; }


    [Display(Name = "Link to Video (URL)")]

    [MaxLength(249)]

    public string AVurl { get; set; }


    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]

    public DateTime PostDate { get; set; }



    [Display(Name = "Date To Archive")]

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]

    public DateTime? StopDate { get; set; }


    [Display(Name = "Posted By")]

    public string PostedBy { get; set; }


    [Display(Name = "Last Edited")]

    public DateTime LastEditDate { get; set; }


    [Display(Name = "Last Edited By")]


翻过高山走不出你
浏览 204回答 1
1回答

温温酱

以下将在所有者所在的位置进行左连接,OwnerName = 'Group A'并在PostType = 'news'可能的情况下进行连接PostId = PostIdvoid Main(){&nbsp; &nbsp; var posts =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; new List<Post>()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Post {PostId = 1, PostType = "news"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Post {PostId = 2, PostType = "old"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Post {PostId = 3, PostType = "news"},&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; var owners =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; new List<OwnerGroup>()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new OwnerGroup {GroupId = 1, PostId = 1, OwnerName = "Group A" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new OwnerGroup {GroupId = 2, PostId = 1, OwnerName = "Group A" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new OwnerGroup {GroupId = 3, PostId = 2, OwnerName = "Group A" },&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; var leftJoinResult = posts&nbsp; &nbsp; &nbsp; &nbsp; .GroupJoin(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; owners.Where(o => o.OwnerName.Equals("Group A")),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r => r.PostId, rp => rp.PostId,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (l1, l2) => new { gjl1 = l1, gjl2 = l2 })&nbsp; &nbsp; &nbsp; &nbsp; .SelectMany(x => x.gjl2.DefaultIfEmpty(), (x, gjl2) => new { x.gjl1, gjl2 })&nbsp; &nbsp; &nbsp; &nbsp; .Where(x => x.gjl1.PostType.Equals("news") )&nbsp; &nbsp; &nbsp; &nbsp; // OPTIONAL: Add this line return the Post matches, not both the Post and the possible left joined OwnerGroup&nbsp; &nbsp; &nbsp; &nbsp; .Select(x => x.gjl1)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // OPTIONAL: Add this line to only get the distinct Post matches&nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(p => p.PostId).Select(grp => grp.First());}public class Post{&nbsp; &nbsp; public int PostId { get; set; }&nbsp; &nbsp; public string PostType { get; set; }}public class OwnerGroup{&nbsp; &nbsp; public int GroupId { get;set; }&nbsp; &nbsp; public int PostId { get; set; }&nbsp; &nbsp; public String OwnerName { get; set; }}
随时随地看视频慕课网APP
我要回答