2

这是我的 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对象有cinemaIDwhich 应该等于oScreenDef["venueId"]and oSynSession["cinemaId"]

为此,我包括两次oPrices["cinemaId"]asC1C2..

我该如何避免这种情况以及如何改进此查询。

请建议更好的方法..

所有listScreenDefinition, listSessionAreaCount,listSynPrices都是List<BsonDocument>mongodb查询中获取的项目..

4

1 回答 1

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 更新说明:将 Item 重命名为 MyItem 以匹配顶部的类。

于 2013-07-22T13:44:20.570 回答