左派加入林克到实体

编程入门 行业动态 更新时间:2024-10-23 10:21:34
本文介绍了左派加入林克到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

另外,如果我们公司决定坚持下去,那么有没有办法在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}

更多推荐

左派加入林克到实体

本文发布于:2023-06-03 10:04:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:左派   实体   林克到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!