Entityframework core query related data filtering

c# entity-framework-core linq

Question

I have the following model

Deal.cs

public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public double? Price { get; set; }

public Guid UserId { get; set; }
public User User { get; set; }

User.cs

public Guid Id { get; set; }
public string Name { get; set; }
public string ResumptionCookie { get; set; }
public List<Deal> Deals { get; set; }

I'm trying to get all users who currently have a deal with a specific code. I could do a get all users and write a big LINQ query but there must a more elegant way.

I read the documentation but it's not quite what I'm looking for and I haven't been able to figure it out.

1
0
3/7/2017 7:32:47 PM

Popular Answer

you can try filtering on the deals first then selecting the user

db.Deals.Where(x=>x.code == "specified code").Select(x=>x.User)

assuming that each user has the deal with "specified code" once if not use .Distenct() or what Akash KC suggested in the comments

0
3/7/2017 6:38:32 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