Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到实体左连接_C#_Entity Framework_Linq To Entities - Fatal编程技术网

C# Linq到实体左连接

C# Linq到实体左连接,c#,entity-framework,linq-to-entities,C#,Entity Framework,Linq To Entities,这是我的疑问: from forum in Forums join post in Posts on forum equals post.Forum into postGroup from p in postGroup where p.ParentPostID==0 select new { forum.Title, forum.ForumID, LastPostTitle = p

这是我的疑问:

from forum in Forums
    join post in Posts on forum equals post.Forum into postGroup    

    from p in postGroup     
    where p.ParentPostID==0

    select new 
    {
        forum.Title,
        forum.ForumID,  
        LastPostTitle = p.Title,
        LastPostAddedDate = p.AddedDate         
    }).OrderBy(o=>o.ForumID) 
当前加入不是左加入,这意味着如果某个论坛没有属于它的帖子,它将不会被返回。
没有帖子的论坛必须返回帖子属性的空(或默认)值

更新

结果集应该是这样的:

ForumId | ForumTitle | LastPostTitle | LastPostAddedDate  
--------+------------+---------------+------------------
4       |   Sport    |    blabla     |       12/4/2010  
4       |   Sport    |    blabla     |       15/4/2010  
6       |   Games    |    blabla     |       1/5/2010  
7       |   Flame    |               |
umposts的公共类
{
公共论坛{get;set;}
公共IQueryable Posts{get;set;}
}
公共类显示集
{
公共字符串名称{get;set;}
公共字符串postile{get;set;}
} 
//左外连接
使用(ClassLibrary1.Entities上下文=新实体())
{
var allForums=来自context.Fora中的f
选择新ForumPosts
{
论坛=f,
Posts=context.Posts.Where(x=>x.ForumId==f.ForumId)
};
List ds=新列表();
foreach(所有论坛中的var论坛)
{
if(forum.Posts.AsEnumerable().Count()!=0)
{
foreach(论坛中的var帖子)
{
添加(新的显示集(){Name=forum.forum.Name,postile=post.PostValue});
}
}
其他的
Add(new DisplaySet(){Name=forum.forum.Name,postile=string.Empty});
}
foreach(ds中的var项)
{
WriteLine(string.Format(“{0}| |{1}”,item.Name,item.postile));
}
}
//这将生成以下正确的LINQ查询
挑选
[Project1][ForumId]作为[ForumId],
[Project1]。[Name]作为[Name],
[Project1][C1]作为[C1],
[Project1].[PostId]作为[PostId],
[Project1][PostValue]作为[PostValue],
[Project1][ForumId1]作为[ForumId1]
从(选择
[extend1][ForumId]作为[ForumId],
[Extent1].[Name]作为[Name],
[Extent2].[PostId]作为[PostId],
[Extent2].[PostValue]作为[PostValue],
[extend2][ForumId]作为[ForumId1],
当([Extent2].[PostId]为NULL)然后强制转换(NULL为int)时的情况,否则1结束为[C1]
来自[dbo].[Forum]作为[Extent1]
[Extent2].[ForumId]=[Extent1].[ForumId]上作为[Extent2]的左外部联接[dbo].[Post]
)AS[Project1]
由[Project1].[ForumId]ASC、[Project1].[C1]ASC订购
此查询生成的结果与

            var allForums = from f in context.Fora  
                            select new ForumPosts  
                            {  
                                Forum = f,  
                                Posts = context.Posts.Where(x=> x.ForumId == f.ForumId)  

下面是一些代码,可以帮助您解决使用链接进行左连接的问题

    private class EntityRole
    {
        public int EntityId { get; set; }
        public int RoleId { get; set; }
    }

    private IList<EntityRole> GetSourceEntityRole()
    {
        var list = new List<EntityRole>() {new EntityRole(){EntityId = 123, RoleId = 1},
                                           new EntityRole(){EntityId = 123, RoleId = 2},
                                           new EntityRole(){EntityId = 123, RoleId = 3},
                                           new EntityRole(){EntityId = 123, RoleId = 4}};

        list.Reverse();

        return list;
    }

    private IList<EntityRole> GetEmptyEntityRole()
    {
        var list = new List<EntityRole>();

        return list;
    }

    public void TestToDelete()
    {
        var source = this.GetSourceEntityRole();
        var destination = this.GetEmptyEntityRole();

        this.TestLeftJoin(source, destination);
    }

    private void TestLeftJoin(IList<EntityRole> source, IList<EntityRole> destination)
    {
        var inserting = this.GetMissing(source, destination);
        var deleting = this.GetMissing(destination, source);

        this.Enumerate("Source", source);
        this.Enumerate("Destination", destination);

        this.Enumerate("Deleting", deleting);
        this.Enumerate("Inserting", inserting);
    }

    private IEnumerable<EntityRole> GetMissing(IList<EntityRole> sourceEntities, IList<EntityRole> destinationEntities)
    {
        return from source in sourceEntities
               join dest in destinationEntities on source.RoleId equals dest.RoleId into joined
               from source2 in joined.DefaultIfEmpty()
               where source2 == null
               select source;
    }

    private void Enumerate(string source, IEnumerable<EntityRole> roles)
    {
        foreach (var item in roles)
        {
            Console.WriteLine("{0}:{1}", source, item.RoleId);
        }
    }
私有类实体
{
public int EntityId{get;set;}
public int RoleId{get;set;}
}
私有IList GetSourceEntityRole()
{
var list=new list(){new EntityRole(){EntityId=123,RoleId=1},
新的EntityRole(){EntityId=123,RoleId=2},
新的EntityRole(){EntityId=123,RoleId=3},
新的EntityRole(){EntityId=123,RoleId=4};
list.Reverse();
退货清单;
}
私有IList GetEmptyEntityRole()
{
var list=新列表();
退货清单;
}
公共void TestToDelete()
{
var source=this.GetSourceEntityRole();
var destination=this.GetEmptyEntityRole();
this.TestLeftJoin(源、目标);
}
私有void TestLeftJoin(IList源、IList目标)
{
var inserting=this.GetMissing(源、目标);
var deleting=this.GetMissing(目的地、来源);
这个。列举(“来源”,来源);
这个。列举(“目的地”,目的地);
本条列举(“删除”,删除);
这个。列举(“插入”,插入);
}
private IEnumerable GetMissing(IList sourceEntities、IList destinationEntities)
{
从sourceEntities中的源返回
将dest加入source.RoleId上的destinationEntities等于dest.RoleId加入已加入
来自joined.DefaultIfEmpty()中的source2
其中source2==null
选择来源;
}
私有void枚举(字符串源,IEnumerable角色)
{
foreach(角色中的变量项)
{
WriteLine(“{0}:{1}”,source,item.RoleId);
}
}

类似的东西。我不太确定,因为我并没有真正了解数据模型,但GroupJoin应该与LEFT OUTER JOIN非常相似,即使它在SQL中并没有实际产生这种效果。

尝试以下方法:

from forum in Forums 
join post in Posts on forum equals post.Forum into postGroup     

// from p in postGroup      
// where p.ParentPostID==0 

select new  
{ 
    forum.Title, 
    forum.ForumID,   
    LastPostTitle = postGroup.FirstOrDefault(p => p.ParentPostID==0).Title, 
    LastPostAddedDate = (DateTime?)postGroup.FirstOrDefault(p => p.ParentPostID==0).AddedDate          
}).OrderBy(o=>o.ForumID)
从左联接返回空的属性也必须为空。那么int=>int?和DateTime=>DateTime?等…

如果没有错误:

var list = from forum in Forums.DefaultItIfEmpty()
from post in Posts.DefaultItIfEmpty()
where forum.forum_id == post.forum_id && post.ParentPostID==0
select new 
{
    forum.Title,
    forum.ForumID,  
    LastPostTitle = p.Title,
    LastPostAddedDate = p.AddedDate         
}).OrderBy(o=>o.ForumID)

你有没有试过这样的方法:

from forum in Forums
from posts in (Posts.Where(qPosts=> forum.ForumId == qPosts.ForumId)).DefaultIfEmpty()
where posts.ParentPostID == 0
orderby forum.ForumId 
select new
{
    forum.Title,
    forum.ForumID,
    LastPostTitle = posts.Title,
    LastPostAddedDate = posts.AddedDate
}

当我得到所有论坛时,我检查帖子数量是否为0,然后我不打印帖子,否则我打印论坛的所有帖子。这就是你想要的吗???为什么不使用join子句?结果集不是我想要的,你的返回“论坛”和所有帖子。但我想返回每个帖子论坛(加入)!您不能在Linq to Entities中使用联接语法。它与Linq to SQL不同,因此,如果您通过SQL探查器查看,此查询将在引擎罩后面生成一个左外部联接。您可以给我一个结果应该是什么样的示例输出吗?请更新答案:添加了一个显示集类,它是最终结果的外观FaultIfEmpty()Net 3.5中的实体框架不支持此功能。我希望看到有助于左键与Zelda连接的代码;)您的
来自postGroup中的p
应该是
来自postGroup中的p。defaultifempty()
from forum in Forums 
join post in Posts on forum equals post.Forum into postGroup     

// from p in postGroup      
// where p.ParentPostID==0 

select new  
{ 
    forum.Title, 
    forum.ForumID,   
    LastPostTitle = postGroup.FirstOrDefault(p => p.ParentPostID==0).Title, 
    LastPostAddedDate = (DateTime?)postGroup.FirstOrDefault(p => p.ParentPostID==0).AddedDate          
}).OrderBy(o=>o.ForumID)
var list = from forum in Forums.DefaultItIfEmpty()
from post in Posts.DefaultItIfEmpty()
where forum.forum_id == post.forum_id && post.ParentPostID==0
select new 
{
    forum.Title,
    forum.ForumID,  
    LastPostTitle = p.Title,
    LastPostAddedDate = p.AddedDate         
}).OrderBy(o=>o.ForumID)
from forum in Forums
from posts in (Posts.Where(qPosts=> forum.ForumId == qPosts.ForumId)).DefaultIfEmpty()
where posts.ParentPostID == 0
orderby forum.ForumId 
select new
{
    forum.Title,
    forum.ForumID,
    LastPostTitle = posts.Title,
    LastPostAddedDate = posts.AddedDate
}