[英]Sql Query to Linq Query
我寫了一個 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.