I have some code, i want to make reusing of conditional, but i don't know how. Do i need dynamic IQueryable or something other? Can you help me make it or find some instruction for it?
DateTime dtTo = DateTime.Now;
DateTime notEndDt = new DateTime(2099, 1, 1);
var result = from o in data
select new
{
id,
Group1Type1result = o.group_id == 1 && o.type== 1 && (NeedActual ? o.dt_end == notEndDt : (o.dt_end == notEndDt || dtTo <= o.dt_end)) ? o.result : 0,
Group1Type2result = o.group_id == 1 && o.type== 2 && (NeedActual ? o.dt_end == notEndDt : (o.dt_end == notEndDt || dtTo <= o.dt_end)) ? o.result : 0,
Group2Type1result = o.group_id == 2 && o.type== 1 && (NeedActual ? o.dt_end == notEndDt : (o.dt_end == notEndDt || dtTo <= o.dt_end)) ? o.result : 0,
Group2Type2result = o.group_id == 2 && o.type== 2 && (NeedActual ? o.dt_end == notEndDt : (o.dt_end == notEndDt || dtTo <= o.dt_end)) ? o.result : 0
};
I want to get smth as it:
int GetResultForGroup(Model.Data o, int groupId, int type)
{
DateTime dtTo = DateTime.Now;
DateTime notEndDt = new DateTime(2099, 1, 1);
return o.group_id == groupId && o.type == type && (NeedActual ? o.dt_end == notEndDt : (o.dt_end == notEndDt || dtTo <= o.dt_end)) ? o.result : 0,
}
var result = from o in data
select new
{
id,
Group1Type1result = GetResultForGroup(o, 1, 1),
Group1Type2result = GetResultForGroup(o, 1, 2),
Group2Type1result = GetResultForGroup(o, 2, 1),
Group2Type2result = GetResultForGroup(o, 2, 2)
};
If i try to make it as in IEnumerable LINQ to Entities does not recognize the method 'Int32 GetResultForGroup(Model.Data, System.DateTime)' method, and this method cannot be translated into a store expression.
upd: I know that i can get everything from BD in IEnumerable and after it use my method, but can i do it in IQueryable? I have paging after query.