1

I have a Group by and Order by in an sql query as below ,how to write the same logic in linq? :-

  GROUP BY bd.bandcharge_desc, bd.rate 
  ORDER BY CASE 
           WHEN bd.bandcharge_desc LIKE '%above%' THEN 
             '1' 
           ELSE 
             '0' 
           END,
           bd.bandcharge_desc,bd.rate ASC
david sam
  • 521
  • 1
  • 8
  • 25
Hamza Zahir
  • 69
  • 1
  • 7
  • Why zero (0) and one (1) is between single qoutes? – Maciej Los Aug 13 '15 at 06:54
  • 1
    possible duplicate of [Group by in LINQ](http://stackoverflow.com/questions/7325278/group-by-in-linq) – Artem Kulikov Aug 13 '15 at 07:02
  • 1
    I tried like this: orderby g.Key.bandcharge_desc Like '*/above/*' ? 1 : 0 descending what I tried is not working, also I don't think it may be a duplicate because the like in case palys an important role in the query. – Hamza Zahir Aug 13 '15 at 07:11

2 Answers2

1

Try this way

orderby bd.bandcharge_desc.Contains("above")?1:0 descending ,
               bd.bandcharge_desc,bd.rate
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
  • it is giving me error: Compiler Error Message: CS1061: 'string' does not contain a definition for 'contain' and no extension method 'contain' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?). using System.Linq is there in c# file. – Hamza Zahir Aug 13 '15 at 07:28
  • I see it is a type, using Contains made it work, thanks. – Hamza Zahir Aug 15 '15 at 21:25
0

A try:

.OrderBy(bd => bd.bandcharge_desc.Contains("above") ? 1 : 0)
.ThenBy(bd => bd.bandcharge_desc)
.ThenBy(bd => bd.rate)
.GroupBy(bd => new {bd.bandcharge_desc, bd.rate})
tafia
  • 1,512
  • 9
  • 18