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.
The original post was:
C# LINQ to Entities query for the intersection of two different properties
I have 3 models named:
- Pencil having Pencil.Id(int) and Pencil.Colors(IEnumerable) Property
- Pen having Pen.Id(int) and Pen.Colors(IEnumerable) Property
- Colors having Id and name.
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.
Raphael‘s great solution is https://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?
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)Else if you only have one pen (in variable
pen)