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

标签 c# asp.net linq linq-to-entities

我无法将此 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;我收到一条错误消息“无法将类型 systems.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")]
    public string LastEditor { get; set; }

    public virtual ICollection<PostAsset> PostAssets { get; set; }

    public virtual ICollection<PostAlbum> PostAlbums { get; set; }

    public virtual ICollection<OwnerGroup> OwnerGroups { get; set; }

最佳答案

如果可能的话,以下将在所有者为 OwnerName = 'Group A'PostType = 'news' 的情况下进行左连接 PostId = PostId

void Main()
{
    var posts = 
        new List<Post>() 
        {
            new Post {PostId = 1, PostType = "news"},
            new Post {PostId = 2, PostType = "old"},
            new Post {PostId = 3, PostType = "news"},
        };

    var owners = 
        new List<OwnerGroup>()
        {
            new OwnerGroup {GroupId = 1, PostId = 1, OwnerName = "Group A" },
            new OwnerGroup {GroupId = 2, PostId = 1, OwnerName = "Group A" },
            new OwnerGroup {GroupId = 3, PostId = 2, OwnerName = "Group A" },
        };

    var leftJoinResult = posts
        .GroupJoin(
            owners.Where(o => o.OwnerName.Equals("Group A")), 
            r => r.PostId, rp => rp.PostId, 
            (l1, l2) => new { gjl1 = l1, gjl2 = l2 })
        .SelectMany(x => x.gjl2.DefaultIfEmpty(), (x, gjl2) => new { x.gjl1, gjl2 })
        .Where(x => x.gjl1.PostType.Equals("news") )
        // OPTIONAL: Add this line return the Post matches, not both the Post and the possible left joined OwnerGroup
        .Select(x => x.gjl1) 
        // OPTIONAL: Add this line to only get the distinct Post matches
        .GroupBy(p => p.PostId).Select(grp => grp.First());
}

public class Post
{
    public int PostId { get; set; }
    public string PostType { get; set; }
}

public class OwnerGroup
{
    public int GroupId { get;set; }
    public int PostId { get; set; }
    public String OwnerName { get; set; }
}

关于c# - 这个左外连接查询的正确 Linq 表达式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845022/

相关文章:

c# - 使用 MVVM 范例用 c# 填充组合框

c# - ASP.NET GridView 焦点选定行

c# - Html.BeginForm MVC3

c# - 带连接的 linq 查询返回不正确的值/关联

.net - 表达式树的确切含义是什么?

c# - 如何以正确的方式将网格绑定(bind)到基于 EF 的 View 模型?

c# - 如何检查对象的一个​​值是否为 null

c# - Linq2Sql 的性能问题

c# - Global.asax 找不到代码隐藏类

vb.net - 获取两个列表之间不匹配的所有成员