简体   繁体   English

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

[英]How to use mulitple conditions in linq C#

This is my Linq query. 这是我的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();

In That oPrices object has cinemaID which should equal to oScreenDef["venueId"] and oSynSession["cinemaId"] . 在那个oPrices对象中, cinemaID应该等于oScreenDef["venueId"]oSynSession["cinemaId"]

For that i'm including two times oPrices["cinemaId"] as C1 and C2 .. 为此,我将oPrices["cinemaId"]C1C2 oPrices["cinemaId"]

how can i avoid that and how can i improve this query. 我如何避免这种情况以及如何改善此查询。

please suggest better way.. 请提出更好的方法..

All listScreenDefinition , listSessionAreaCount , listSynPrices are List<BsonDocument> items getting it from mongodb queries.. 所有listScreenDefinitionlistSessionAreaCountlistSynPrices都是List<BsonDocument>项,可从mongodb查询中获取。

You can use let (see MSDN ) and remove the new { c1 = ... } . 您可以使用let (请参阅MSDN )并删除new { c1 = ... } But I don't see much more optimization: 但是我看不到更多的优化方法:

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();

Update 更新

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 update note: renamed Item to MyItem to match the class at the top. rtpHarry更新说明:将Item重命名为MyItem以匹配顶部的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM