这是我的Linq查询。
var tmp = (from oScreenDef in listScreenDefinition
join oSynSession in listSynSession on
new { c1 = oScreenDef["venueCd"], c2 = oScreenDef["screenBytNum"] }
equals new { c1 = oSynSession["cinemaId"], c2 = oSynSession["screenDetails"].AsBsonDocument["num"] }
join oSessionAreaCount in listSessionAreaCount on
new { c1 = oScreenDef["venueCd"] }
equals new { c1 = oSessionAreaCount["cinemaId"] }
join oPrices in listSynPrices on
new { c1 = oScreenDef["venueCd"], c2 = oSynSession["cinemaId"] }
equals new { c1 = oPrices["cinemaId"], c2 = oPrices["cinemaId"] }
select new { doc = oSynSession[0], oScreenDef }).ToList();
在oPrices
对象中cinemaID
应该等于oScreenDef["venueId"]
和oSynSession["cinemaId"]
。
因为我包括oPrices["cinemaId"]
两次C1
和C2
..
我该如何避免这种情况?如何改进此查询。
请建议更好的方式..
所有listScreenDefinition
,listSessionAreaCount
,listSynPrices
都是从List<BsonDocument>
次查询中获取的mongodb
个项目。
答案 0 :(得分:0)
您可以使用let
(请参阅MSDN)并移除new { c1 = ... }
。但我没有看到更多的优化:
var tmp = (from oScreenDef in listScreenDefinition
let oScreenDefVenueCd = oScreenDef["venueCd"] // let clause creates a "local variable" Inside a LINQ query
join oSynSession in listSynSession on
new { c1 = oScreenDefVenueCd, c2 = oScreenDef["screenBytNum"] }
equals new { c1 = oSynSession["cinemaId"], c2 = oSynSession["screenDetails"].AsBsonDocument["num"] }
join oSessionAreaCount in listSessionAreaCount on
oScreenDefVenueCd
equals oSessionAreaCount["cinemaId"]
join oPrices in listSynPrices on
new { c1 = oScreenDefVenueCd, c2 = oSynSession["cinemaId"] }
equals new { c1 = oPrices["cinemaId"], c2 = oPrices["cinemaId"] }
select new { doc = oSynSession[0], oScreenDef }).ToList();
public class MyItem
{
public SynSession Doc;
public ScreenDef ScreenDef;
public MyItem(SynSession doc, ScreenDef screenDef)
{
Doc = doc;
ScreenDef = screenDef;
}
}
List<MyItem> tmp = (from oScreenDef in listScreenDefinition
let oScreenDefVenueCd = oScreenDef["venueCd"] // let clause creates a "local variable" Inside a LINQ query
join oSynSession in listSynSession on
new { c1 = oScreenDefVenueCd, c2 = oScreenDef["screenBytNum"] }
equals new { c1 = oSynSession["cinemaId"], c2 = oSynSession["screenDetails"].AsBsonDocument["num"] }
join oSessionAreaCount in listSessionAreaCount on
oScreenDefVenueCd
equals oSessionAreaCount["cinemaId"]
join oPrices in listSynPrices on
new { c1 = oScreenDefVenueCd, c2 = oSynSession["cinemaId"] }
equals new { c1 = oPrices["cinemaId"], c2 = oPrices["cinemaId"] }
select new MyItem(oSynSession[0], oScreenDef)).ToList();
rtpHarry更新说明:将项目重命名为MyItem以匹配顶部的类。