-3

I have written an sql qyery like

SELECT TOP 1000 
  [tlotWeight]
  ,[TEU]
  ,[SeaAirFlag]
  ,CASE 
  WHEN [SeaAirFlag]='SEA' OR [SeaAirFlag]='Sea' 
  then [TEU]  
  else [tlotWeight] end as Volume
FROM [LogisticsBI].[dbo].[GoldenVolume]

I want it to convert it to linq c# query,I have tried something like this

(from t in db.GoldenVolumes
 select new { Volume=(t.SeaAirFlag=="SEA"|| t.SeaAirFlag=="Sea")?t.TEU: t.tlotWeight)}
 ).Take(10000).Distinct()

but its showing some syntax error in linqpad Please help me to correct way in linq to write this query

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
gaup
  • 129
  • 1
  • 11
  • 3
    "*but its showing some syntax error*" - what exactly error you're getting? There are no mind-readers here.... – Andrey Korneyev Sep 22 '15 at 12:36
  • error is : Syntax error, ',' expected ) expected – gaup Sep 22 '15 at 12:38
  • Use LINQPad to Help ease into LINQ syntax https://en.wikipedia.org/wiki/LINQPad – Harvey Sep 22 '15 at 12:38
  • `(from t in db.GoldenVolumes select new { Volume = (t.SeaAirFlag=="SEA"|| t.SeaAirFlag=="Sea") ? t.TEU : t.tlotWeight }).Take(10000).Distinct();` – Rob Sep 22 '15 at 12:41
  • I have use LINQPad in that only it is showing syntax error Syntax error, ',' expected ) expected – gaup Sep 22 '15 at 12:41
  • 1
    Small typos don't really belong on SO. You should have been able to find the mistake if you spent a few minutes yourself. – Rob Sep 22 '15 at 12:44
  • looks like you got confused with all those brackets around – Rohit Sep 22 '15 at 13:00

2 Answers2

0

Well, I see problem with brackets here (opened one time and closed two times):

select new { Volume = (t.SeaAirFlag=="SEA" || t.SeaAirFlag=="Sea") ? t.TEU : t.tlotWeight)}

It looks like this should be either

select new { Volume = ((t.SeaAirFlag=="SEA" || t.SeaAirFlag=="Sea") ? t.TEU: t.tlotWeight)}

or (it's up to you)

select new { Volume = (t.SeaAirFlag=="SEA" || t.SeaAirFlag=="Sea") ? t.TEU: t.tlotWeight }
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
0

The error is pretty straightforward you have misarranged your round brackets.

t.tlotWeight)}) is wrong instead it should be t.tlotWeight})

Its better to split them to next lines for more clearity.

    var res = (from t in db.GoldenVolumes
                   select new
                   {
                       Volume = (t.SeaAirFlag.ToUpperInvariant().Contains("SEA")) ?
                           t.TEU  : t.tlotWeight
                   })

                   .Take(10000)
                   .Distinct();
Rohit
  • 10,056
  • 7
  • 50
  • 82