c# - Equivalent of 'WHERE columnname IN' SQL syntax in Entity Framework -
i have scenario
public class timesheet { [key] public int timesheetid { get; set; } public string username { get; set; } } public class approval { [key] public int approvalid { get; set; } [index(isunique = true)] [stringlength(450)] public string approver { get; set; } public virtual icollection<approvaldetail> details { get; set; } } public class approvaldetail { [key] public int approvaldetailid { get; set; } [stringlength(450)] public string username { get; set; } }
i want following syntax in ef.
select * timesheet username in (select [ad].username approval [a] inner join approvaldetail [ad] on [a].approvalid = [ad].approvalid [a].approver = 'warheat1990')
how achieve this?
update :
my repo
public ienumerable<timesheet> list() { return _timesheet.asenumerable().tolist(); } public ienumerable<timesheet> listbyusername(string username) { return _timesheet.where(w => w.username == username).tolist(); }
this should it:
var usernamesbyapprover = approvals .where(a => a.approver == "warheat1990") .selectmany(a => a.details.select(d => d.username)); var timesheetsbyapprover = timesheets .where(t => usernamesbyapprover.contains(t.username));
note if query split 2 expressions, entity framework convert single sql query once evaluate timesheetsbyapprover
variable because of deferred execution.
Comments
Post a Comment