C#LINQ到Entities-属性上的对象的相交和对象的集合 [英] C# LINQ to Entities- Properties on the intersection of an object and a collection of objects

查看:159
本文介绍了C#LINQ到Entities-属性上的对象的相交和对象的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我常常问这是旨在了解如何获得在其正与一个我持有相同的属性,另一组的机型了类似的问题。

I used to ask a similar question which was aiming to understand how to get the models at the other set which are having the same property with the one I am holding.

现在的问题是:
那么,究竟是什么这实际上引导我到另一组类似财产的名称

Now the problem is: So, what was the name of the "similar property" which actually guided me to the other set.

原帖是:
C#LINQ到实体查询的两种不同性质

我有3个型号命名为:


  1. 铅笔有Pencil.Id(int)和Pencil.Colors(IEnumerable的)房产

  2. 笔有Pen.Id(int)和Pen.Colors(IEnumerable的)房产

  3. 有标识和名称的颜色。

颜色模型的IEnumerable,因此有更多的大于1的颜色。例如;笔有15个不同的颜色和铅笔有25种不同的颜色。我希望把corresonding铅笔如果我握住笔的颜色之一,也是在铅笔的调色板avaialable。

Colors model is IEnumerable so it has more than 1 color. For example; the pen has 15 different colors and pencil is having 25 different colors. I want to bring the corresonding pencil if one of the colors of the pen that I am holding is also avaialable in the color palette of that pencil.

拉斐尔的伟大的解决方案是 http://stackoverflow.com/a/11722191/1062284

Raphael's great solution is http://stackoverflow.com/a/11722191/1062284

int id = id_of_the_pen_that_i_am_holding;
Pen p = db.Pens.Find(id);
var penColorIds = p.Color.Select(m => m.Id).ToList();
var list = db.Pencils.Where(pencil => pencil.Color.Any(color => penColorIds.Contains(color.Id));

因此,它是确定,像一个魅力的工作,但如何对学习的常用颜色的名称?
我们得到的其他属性而持有相同颜色,但是那是什么颜色的?

So it is OK and working like a charm but how about learning the name of the common colors? We get the other properties which are holding the same color but what was that color?

我将不胜感激,如果有人能产生这种LINQ查询。我对LINQ非常新,并会非常感激的任何帮助。

I will appreciate if some one can produce this LINQ query. I am pretty new for LINQ and will much appreciate any help.

推荐答案

如果你在你的手里拿着多笔再使用(变量

If you are holding multiple pens in your hand then use (in variable pens)

var colors = pens.SelectMany(p=>p.Colors).Distinct(); 
var commonColors = db.Pencils.SelectMany (p => p.Colors.Where(c=>colors.Contains(c))).Distinct()

否则,如果你只有一个笔(变量

Else if you only have one pen (in variable pen)

var colors = pen.Colors;
var commonColors = db.Pencils.SelectMany (p => p.Colors.Where(c=>colors.Contains(c))).Distinct()

这篇关于C#LINQ到Entities-属性上的对象的相交和对象的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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