C# 此左外部联接查询的正确Linq表达式是什么?
我很难将这个his sql查询转换为linq表达式,该表达式将结果作为类的IEnumerable返回 以下是查询:C# 此左外部联接查询的正确Linq表达式是什么?,c#,asp.net,linq,linq-to-entities,C#,Asp.net,Linq,Linq To Entities,我很难将这个his sql查询转换为linq表达式,该表达式将结果作为类的IEnumerable返回 以下是查询: Select * from Posts left outer join Ownergroups on Posts.PostId=Ownergroups.PostID Where Ownergroups.OwnerName = 'Group A' AND PostType = 'news' 这是唯一一个不抛出错误的表达式,但它也只返回一个结果 NewsViewMo
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 list转换为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; }
公共类职位
{
公共int PostId{get;set;}
公共字符串PostType{get;set;}
[显示(Name=“Top Title”)]
[最大长度(300)]
公共字符串Headline1{get;set;}
[显示(Name=“Subtitle”)]
[最大长度(300)]
公共字符串Headline2{get;set;}
公共字符串Headline3{get;set;}
[显示(Name=“按组织或人员”)]
[最大长度(250)]
公共字符串署名{get;set;}
[显示(Name=“文章文本”)]
[最大长度(4999)]
[allowtml]
公共字符串PostText1{get;set;}
[显示(名称=“附加文本”)]
[最大长度(4999)]
[allowtml]
公共字符串PostText2{get;set;}
公共字符串AuthorGroup{get;set;}
[显示(Name=“链接到视频(URL)”)]
[最大长度(249)]
公共字符串AVurl{get;set;}
[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString=“{0:d}”)]
public DateTime PostDate{get;set;}
[显示(Name=“存档日期”)]
[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString=“{0:d}”)]
公共日期时间?停止日期{get;set;}
[显示(Name=“发布人”)]
由{get;set;}发布的公共字符串
[显示(Name=“上次编辑”)]
公共日期时间LastEditDate{get;set;}
[显示(Name=“上次编辑人”)]
公共字符串LastEditor{get;set;}
公共虚拟ICollection PostAssets{get;set;}
公共虚拟ICollection PostAlbums{get;set;}
公共虚拟ICollection所有者组{get;set;}
以下内容将在所有者为OwnerName='groupa'
和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; }
}
void Main()
{
var员额=
新名单()
{
新帖子{PostId=1,PostType=“news”},
新帖子{PostId=2,PostType=“old”},
新帖子{PostId=3,PostType=“news”},
};
var所有者=
新名单()
{
新所有者组{GroupId=1,PostId=1,OwnerName=“Group A”},
新所有者组{GroupId=2,PostId=1,OwnerName=“Group A”},
新所有者组{GroupId=3,PostId=2,OwnerName=“Group A”},
};
var leftJoinResult=posts
.GroupJoin(
其中(o=>o.OwnerName.Equals(“A组”),
r=>r.PostId,rp=>rp.PostId,
(l1,l2)=>新的{gjl1=l1,gjl2=l2})
.SelectMany(x=>x.gjl2.DefaultIfEmpty(),(x,gjl2)=>new{x.gjl1,gjl2})
其中(x=>x.gjl1.PostType.Equals(“新闻”))
//可选:添加此行将返回Post匹配项,而不是Post和可能的左连接所有者组
.选择(x=>x.gjl1)
//可选:添加此行以仅获取不同的Post匹配项
.GroupBy(p=>p.PostId).Select(grp=>grp.First());
}
公营职位
{
公共int PostId{get;set;}
公共字符串PostType{get;set;}
}
公共类所有者组
{
public int GroupId{get;set;}
公共int PostId{get;set;}
公共字符串所有者名称{get;set;}
}
您正在进行内部联接。是否尝试过“以类的IEnumerable形式返回结果的linq表达式”哪个类?我们可以看到它吗?因为SQLselect*
从两个表返回字段,所以为了对LINQ执行相同的操作,您需要定义一些具有所有这些属性的类。换句话说,您需要在IEnumerable
中返回什么类型的T
?顺便说一句,Where Ownergroups.OwnerName='groupA'
>有效地将左侧外部连接
转换为内部连接
。这同样有效:选择Posts.*从Posts左侧外部连接Posts上的Ownergroups.PostId=Ownergroups.PostId和Ownergroups.Ownergroups.OwnerName='Group A'其中PostType='News'也许我可以帮助你?谢谢@Carlo Bos!我正在使用你非常优雅的示例。我已经调试程序告诉我“只能对Type.IsGenericParameter为true的类型调用方法”我不知道这适用于什么。因为今年夏天我的时间非常紧迫,我最终写了一个SqlQuery,这是我以前从未做过的,用了不到一分钟的时间。我将奖励这个答案,因为我认为通过对模型或表达式的一些调整,它会起作用。这是一个很棒的学习工具!