目前想寫出將此查詢改為LINQ的寫法
SELECT top 1 type.id, type.tw, d.id as did, d.name, d.create_date, d_detail.text, type.is_global
FROM type
left join d ON type.id = d.type_id and d.company_id=1
left join d_detail ON d_detail.d_id = d.id
WHERE type.id = 1
ORDER BY d.id DESC
UNION
SELECT top 1 type.id, type.tw, d.id as did, d.name, d.create_date, d_detail.text, type.is_global
FROM type
left join d ON type.id = d.type_id
left join d_detail ON d_detail.d_id = d.id
WHERE type.id = 2
調整後目前為
var q = from c in db.types
join o in db.d on c.id equals o.id
join r in db.d_detail on o.id equals r.d_id
where c.id == 1
orderby o.id
select new {
c.id,
c.tw,
o.id,
o.name,
o.company_id,
r.text,
c.is_global
};
但select new內的o.id會報錯
錯誤訊息為匿名類型不可有多個同名屬性
研究後推測c.id與o.id衝突
想請問各位大大
LINQ有可以設定別名之類的作法或是其他如何處理該問題之方法嗎?
另外這樣設定與上面SQL設定取之值是否相同呢?
感謝看完
改這樣?
select new {
c.id,
c.tw,
id2 = o.id,
o.name,
o.company_id,
r.text,
c.is_global
};
https://www.codeproject.com/Questions/309799/Alias-column-names-for-a-linq-query
select new {
aliasName01 = c.id,
aliasName02 = c.tw,
}