pwas pwas - 18 days ago 5
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

You can use a inner select:

var found = dbContext.Items.Where(off => off.Price > dbContext.Items.FirstOrDefault(x => x.Id == id).Price);
Comments