sql server - LINQ equivalent of my SQL Query for UNION ALL -
(select * sheethay sheetstatus = 2) union all(select * sheethay sheetstatus = 1) union (select * sheethay sheetstatus = 0) union all(select * sheethay sheetstatus= 3)
i result set this:
i mean '2' together, '0' together, '3' ( no '1' in table yet). when use linq , union them see result on grid in order of sheetid, primary key. mean see order of sheets displayed 15,23,25,27,28,29 etc. want sql result set.23,43,25,28 etc
ienumerable<sheethay> listtwos = get(linq query twos); ienumerable<sheethay> listones = get(linq query ones); ienumerable<sheethay> listzeros = get(linq query zeros); ienumerable<sheethay> listthrees = get(linq query threes); .... return listtwos.union(listzeros).union(listones).union(listthrees);
let me know if need other information. thanks.
you don't need use multiple queries can use case
in order by
in sql , similar way in linq.
sql:
select * sheethay sheetstatus in(0,1,2,3)) order case sheetstatus when 2 1 when 1 2 when 0 3 when 3 4 end asc, sheetstatus asc
linq:
int[] status = {0, 1, 2, 3}; var query = db.sheethay .where(s => status.contains(s.sheetstatus)) .orderbydescending(s => s.sheetstatus == 2) .thenbydescending(s => s.sheetstatus == 1) .thenbydescending(s => s.sheetstatus == 0) .thenbydescending(s => s.sheetstatus == 3) .thenby(s => s.sheetstatus);
descending because comparison returns bool
, true
"higher" false
(1/0).
you use conditional operator return int
ordering:
var query = db.sheethay .where(s => status.contains(s.sheetstatus)) .orderby(s => s.sheetstatus == 2 ? 0 : 1) .thenby(s => s.sheetstatus == 1 ? 0 : 1) .thenby(s => s.sheetstatus == 0 ? 0 : 1) .thenby(s => s.sheetstatus == 3 ? 0 : 1) .thenby(s => s.sheetstatus);
Comments
Post a Comment