Sabir Hossain
Sabir Hossain

Reputation: 1205

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

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

Upvotes: 0

Views: 75

Answers (1)

Tomato32
Tomato32

Reputation: 2245

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();

Upvotes: 1

Related Questions