How to select an entity from database based on nested list item match?

entity-framework-core linq

Question

I have a DbSet that contains a list of Item, Now I want to search an Item from the database based on its nested list item matching.

Item Model

public int ItemID{ get; set; }
public string Cover { get; set; }
public List<SlideModel> Slides { get; set; }

Slide Model

public int SlideID{ get; set; }
public int ItemID{ get; set; }
public string Slide{ get; set; }

Now I will pass a search string of Slide and it will search for the Item who have the Slide in its List<SlideModel> and return the Item

item = await context.Items
    .Include(i => i.Slides)
    .Where(...todo-maybe...)
    .FirstOrDefaultAsync();

How should I write the Query method to get the item based on the slide

1
0
9/9/2018 12:14:57 PM

Accepted Answer

That's the thing that you want? Hope to help, my friend :))

string inputSlide = "abc";

item = await context.Items
    .Include(i => i.Slides)
    .Where(i => i.Slides.Any(i => i.Slide.ToLower() == inputSlide.ToLower()))
    .FirstOrDefaultAsync();
2
9/9/2018 5:32:35 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow