如何在linq C#中使用多个条件

时间:2013-07-22 13:20:24

标签: c# linq

这是我的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"]两次C1C2 ..

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

请建议更好的方式..

所有listScreenDefinitionlistSessionAreaCountlistSynPrices都是从List<BsonDocument>次查询中获取的mongodb个项目。

1 个答案:

答案 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以匹配顶部的类。