Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Sql查询到Linq查询_C#_Linq_Sql To Linq Conversion - Fatal编程技术网

C# Sql查询到Linq查询

C# Sql查询到Linq查询,c#,linq,sql-to-linq-conversion,C#,Linq,Sql To Linq Conversion,我写了一个类似sql的qyery SELECT TOP 1000 [tlotWeight] ,[TEU] ,[SeaAirFlag] ,CASE WHEN [SeaAirFlag]='SEA' OR [SeaAirFlag]='Sea' then [TEU] else [tlotWeight] end as Volume FROM [LogisticsBI].[dbo].[GoldenVolume] 我想将其转换为linq c查询,我已经尝试过类似的方法

我写了一个类似sql的qyery

SELECT TOP 1000 
  [tlotWeight]
  ,[TEU]
  ,[SeaAirFlag]
  ,CASE 
  WHEN [SeaAirFlag]='SEA' OR [SeaAirFlag]='Sea' 
  then [TEU]  
  else [tlotWeight] end as Volume
FROM [LogisticsBI].[dbo].[GoldenVolume]
我想将其转换为linq c查询,我已经尝试过类似的方法

(from t in db.GoldenVolumes
 select new { Volume=(t.SeaAirFlag=="SEA"|| t.SeaAirFlag=="Sea")?t.TEU: t.tlotWeight)}
 ).Take(10000).Distinct()
但是它在linqpad中显示了一些语法错误
请帮助我在linq中纠正编写此查询的方式好吧,我发现括号有问题,打开一次,关闭两次:

select new { Volume = (t.SeaAirFlag=="SEA" || t.SeaAirFlag=="Sea") ? t.TEU : t.tlotWeight)}
看起来这应该是

select new { Volume = ((t.SeaAirFlag=="SEA" || t.SeaAirFlag=="Sea") ? t.TEU: t.tlotWeight)}
还是由你决定

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

错误很简单,因为您的圆括号排列错误

t、 tlotWeight}错误,应为t.tlotWeight}

最好将它们拆分到下一行,以便更清晰

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

                   .Take(10000)
                   .Distinct();

但它显示了一些语法错误-您到底得到了什么错误?这里没有读心术的人……错误是:语法错误,,“期望使用LINQPad帮助简化db中t的LINQ语法。GoldenVolumes选择new{Volume=t.SeaAirFlag==SEA | | t.SeaAirFlag==SEA?t.TEU:t.tlotWeight}.Take10000.Distinct;小的打字错误不适合这样的人。如果你自己花几分钟的话,你应该能够找到错误。