左派加入林克到实体 [英] Left Outer Join in Linq-To-Entities

查看:135
本文介绍了左派加入林克到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



另外,如果我们公司决定坚持下去,那么有没有办法在linq-to-entities中做左边的外连接尽管使用linq-to-entity,尽管目前存在其中的缺陷,但您认为获取Visual Studio 2010非常重要吗?换句话说,Visual Studio 2010中有什么可以帮助开发不在Visual Studio 2008中的实体?



谢谢。

解决方案

这可能无法满足您的需求,因为对象未被正确平坦化,但您可以执行外部连接并返回一组匿名类型。



左外连接与内连接和except set(第一个集合不连接到第二个集合的部分)的并集相同。



这里我只是简单地




  • 计算内部连接

  • 计算除设置(创建一个空B实体以确保类型一致)

  • 组合两组。



LINQ不漂亮,但很可爱。

  class A 
{
public int Id {get;组; }
public string PropertyA {get;组; }
}

class B
{
public int Id {get;组; }
public string PropertyB {get;组; }
}

var aThings = new List< A>();
var bThings = new List< B>();

var innerJoin = aThings.SelectMany(a =>
bThings.Where(b => a.Id == b.Id).Select(b => new {a ,b}))。ToList();

var exceptSet = aThings.Where(a =>
!bThings.Select(b => b.Id).Contains(a.Id))。选择(a => ;
{
B b = new B();
返回新的{a,b};
});

var outerJoin = innerJoin;
outerJoin.AddRange(exceptSet);

结果是匿名类型列表{a,b}


Is there a way to do a left outer join in linq-to-entities WITHOUT having tables mapped with foreign keys?

Also, if our company decides to stick with using linq-to-entities despite all of its current flaws, do you think it's important to get Visual Studio 2010? In other words, what is in Visual Studio 2010 that would help developing with entities that isn't in Visual Studio 2008?

Thanks.

解决方案

This may not satisfy you because the objects are not properly flattened, but you can perform an outer join and return a collection of anonymous types.

A left outer join is the same as the union of the inner join and the except set (the part of the first set which does not join onto the second set).

Here I simply

  • calculate the inner join
  • calculate the except set (creating an empty B entity to ensure the types are consistent)
  • combine the two sets.

The LINQ is not beautiful, but it is cute.

class A
{
    public int Id { get; set; }
    public string PropertyA { get; set; }        
}

class B
{
    public int Id { get; set; }
    public string PropertyB { get; set; }
}   

var aThings = new List<A>();
var bThings = new List<B>();

var innerJoin = aThings.SelectMany(a => 
    bThings.Where(b => a.Id == b.Id).Select(b => new { a, b })).ToList();

var exceptSet = aThings.Where(a => 
    !bThings.Select(b => b.Id).Contains(a.Id)).Select( a =>
    {
        B b = new B();
        return new { a, b };
     });

var outerJoin = innerJoin;
outerJoin.AddRange(exceptSet);

The result is a List of anonymous types {a, b}

这篇关于左派加入林克到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆