How to write a Linq to Entities query given a list of entities as parameter

Asier 41 Reputation points
2021-10-26T20:35:14.313+00:00

I have 2 tables: Materials and Colors with two colums

Materials (guid and name)
Colors (guid and name)
One Material can have a lot of colors and a Color can be in a lot of Materials. (many to many relationship)

Using LINQ I can write Material.colors and this send me a list of the entities of color contained in that material.

I would like to make function in which I pass a list of Color and return what materials have those Colors.

I am writing in VB.net

Public Shared Function getMaterialsFromColor(vColors As List(Of Entities.Color)) As List(Of Entities.Materials)

Dim _materials As New List(Of Entities.Materials)

_materials.AddRange(From vmaterial as Entities.Materials where vmaterial.color.name=)

'(this is the part that I dont know)

end function
thanks a lot

Developer technologies | .NET | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 126.1K Reputation points
    2021-10-26T20:47:41.173+00:00

    If you want to compare the colours by name, then try something like this:

    Public Function getMaterialsFromColor(vColors As List(Of Entities.Color)) As List(Of Entities.Materials)
    
        Return vmaterial.Where(Function(m) vColors.Any(Function(c) c.Name = m.Color.Name)).ToList
    
    End Function
    

  2. Viorel 126.1K Reputation points
    2021-10-26T21:18:10.237+00:00

    If a Material contains a list of colours called Colors, then check this function:

    Public Function getMaterialsFromColor(vColors As List(Of Entities.Color)) As List(Of Entities.Materials)
    
        Return vmaterial.Where(Function(m) m.Colors.Any(Function(mc) vColors.Any(Function(c) c.Name = mc.Name))).ToList
    
    End Function
    

    If this cannot be adjusted, maybe show the VB definitions of your types.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.