pwas pwas - 1 year ago 74
C# Question

Entity framework LINQ - access entity field w/o fetching it

Consider I have simple entity:

public class Item
public int Id { get; set; }
public decimal Price { get; set; }

Consider I have got id only of item and and want to find all entities that price is greater than price in given item.

I can do:

var item = dbContext.Items.Find(id);
var found = dbContext.Items.Where(off => off.Price > item.Price);

Can I do this w/o fetching item from database?

Answer Source

You can use a inner select:

var found = dbContext.Items.Where(off => off.Price > dbContext.Items.FirstOrDefault(x => x.Id == id).Price);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download