Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq查询优化与方法语法等价_Linq_Entity Framework_Postgresql 9.2_Npgsql - Fatal编程技术网

Linq查询优化与方法语法等价

Linq查询优化与方法语法等价,linq,entity-framework,postgresql-9.2,npgsql,Linq,Entity Framework,Postgresql 9.2,Npgsql,我有两个疑问: 第一个得到一个标识符的不同列表 第二个过滤器在这些ID上过滤结果 var query = (from bk in context.BookKeeping join bk12 in context.BookKeeping on bk.LetteringId equals bk12.LetteringId into bk11 from bk1 in bk11.Default

我有两个疑问:

  • 第一个得到一个标识符的不同列表
  • 第二个过滤器在这些ID上过滤结果

        var query = (from bk in context.BookKeeping
                     join bk12 in context.BookKeeping on bk.LetteringId equals bk12.LetteringId 
                     into bk11
                     from bk1 in bk11.DefaultIfEmpty()
                     join bi2 in context.BillingInformation on bk1.BillingInformationId equals bi2.BillId 
                     into bi1
                     from bi in bi1.DefaultIfEmpty()
                     where bk.LetteringId != null && bk.PaymentId != null && bk1.LetteringId != null
                     select bk.PaymentId).Distinct();
    
        var query2 = from m in context.Movement
                     join p2 in context.Payment on m.PaymentId equals p2.Id
                     into p1
                     from p in p1.DefaultIfEmpty()
                     join pm2 in context.PaymentMode on p.PaymentModeId equals pm2.Id
                     into pm1
                     from pm in pm1.DefaultIfEmpty()
                     from ids in query
                     where ids.Value == p.Id
                     select m;
    
  • 第二个查询的有趣部分如下:

                     from ids in query
                     where ids.Value == p.Id
    
    我想知道是否可以使它更紧凑,以及如何使用方法语法有条件地筛选ID列表。我知道我必须使用SelectMany,但不知道如何选择正确的重载

    蒂亚

    我找到了解决方案:

    var query2 = context.Movement.SelectMany(m => query1, (m, ids) => new { m, ids })
        .Where(a => a.m.PaymentId == a.ids).Select(a => a.m);
    

    我必须说,第一个lamba的语法一开始很奇怪

    你没有考虑在SQL中建立所有的登录,而不是强制LINQ把所有的工作人员翻译成SQL。内联表值函数可以被认为是一个很好的实践。我忘记添加一个重要的标记:数据库是PostgreSQL。