简体   繁体   English

Linq优化查询的记录与另一个表不匹配

[英]Linq Optimizing query with records not matching from another table

I am having following query which pulls data from a table with records that should not be present in another table. 我正在执行以下查询,该查询从具有不应该存在于另一个表中的记录的表中提取数据。

The query is working perfectly but it is taking too much time & performance is affected tremendously. 该查询工作正常,但是花费太多时间,并且性能受到极大影响。

What changes can i make to this query to get better performance or should i be doing this in another way? 我可以对此查询进行哪些更改以获得更好的性能,还是应该以其他方式执行此操作?

var data = (from A in ctx.tblMachine
where
     A.CompanyId == companyId &&
     A.InOutDate >= tempDt &&
     A.InOutDate <= toDate &&
!(from B in ctx.tblEntry
where
     B.CompanyId == companyId &&
     A.EmployeeId == B.EmployeeId &&
     A.InOutDate == B.EntryDate &&
     B.EntryMethod == "M"
select new
{
     B.EmployeeId
}).Contains(new { EmployeeId = A.EmployeeId })
orderby
A.EmployeeId, A.InOutDate select new
{
     A.EmployeeId,
     A.InOutDate,
     A.InOutFlag,
     A.InOutTime
}).ToList();

You can try with join also instead of inner query... You can use like this make change as per your need.. 您也可以尝试使用join而不是内部查询...您可以根据需要使用这种make change。

var data = (from A in ctx.tblMachine
        join B in ctx.tblEntry on A.EmployeeId == B.EmployeeId &&
                                    A.InOutDate == B.EntryDate &&
                                    B.CompanyId == companyId &&
                                    B.EntryMethod == "M" 
        where
                A.CompanyId == companyId &&
                A.InOutDate >= tempDt &&
                A.InOutDate <= toDate &&
                !(B.EmployeeId).Contains(new { EmployeeId = A.EmployeeId })
        orderby
        A.EmployeeId, A.InOutDate
        select new
        {
            A.EmployeeId,
            A.InOutDate,
            A.InOutFlag,
            A.InOutTime
        }).ToList();

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

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