简体   繁体   English

将SQL查询更改为LINQ,asp.net MVC

[英]Change SQL Query to LINQ, asp.net MVC

How to change this SQL query to LINQ? 如何将此SQL查询更改为LINQ? I've tried it several times, but it didn't work 我试过几次了,但是没用

SELECT Payment.ID, Payment.TotalGroupID, PaymentTrans.PaymentID, PaymentTrans.TotalGroupID as TotalGroupID1, PaymentTrans.TransferStatus
FROM PaymentTrans INNER JOIN Payment
ON (PaymentTrans.PaymentID = Payment.ID OR PaymentTrans.TotalGroupID = payment.TotalGroupID)
WHERE (PaymentTrans.TransferStatusis NULL  OR (PaymentTrans.TransferStatus <> '01' and PaymentTrans.TransferStatus <> '02'))

and this is my try 这是我的尝试

var a= (from x in db.PaymentTransactions
        join p in db.Payments
        on
        x.PaymentID equals p.ID
        where x.TransferStatus== null || (x.TransferStatus!= "01" && x.TransferStatus!= "02")
                            select new { x, p }).ToList();

but it still wrong LINQ, because in my query I have 2 conditions in ON Clause. 但是它仍然错误LINQ,因为在我的查询中,我在ON子句中有2个条件。 thanks 谢谢

try this 尝试这个

var query = (from x in db.PaymentTransactions
             join p in db.Payments 
             on x.PaymentID equals p.ID //main condition of join
             where  ((x.TransferStatus == null || 
             (x.TransferStatus != "01" && x.TransferStatus!= "02")) //your `where` condition
             || x.TotalGroupID == p.TotalGroupID) //your second or join
             select new {x,p})
            .ToList();

You cannot add multiple ON in LINQ. 您不能在LINQ中添加多个ON。 the solution of your problem above can be solved like this. 您上面问题的解决方案可以这样解决。

Hint : just use multiple Where. 提示 :只需在多个位置使用。

var result = 
(
    from trans in db.PaymentTransactions
    join payment in db.payments
    on trans.PaymentID equals payment.ID
    where trans.TotalGroupID == payment.TotalGroupID
    where x.TransferStatus== null || (x.TransferStatus!= "01" && x.TransferStatus!= "02")
    select new 
    {
        //your properties
    }

).ToList();

The answers above filter on both conditions, they should filter one of the conditions according to the question (PaymentID or TotalPaymentID). 上面的答案会同时过滤两种情况,它们应根据问题(PaymentID或TotalPaymentID)过滤条件之一。 You can either write two seperate queries and use a union or use a Cartesian product before filtering. 您可以在过滤之前编写两个单独的查询并使用并集或使用笛卡尔积。

var result = (from paymentTransaction in db.PaymentTransactions
                      join payment in db.Payments on paymentTransaction.PaymentID equals payment.ID
                      where paymentTransaction.TransferStatus == null || (paymentTransaction.TransferStatus != "01" && paymentTransaction.TransferStatus != "02")
                      select new { paymentTransaction, payment }).Union
                     (from paymentTransaction in db.PaymentTransactions
                      join payment in db.Payments on paymentTransaction.TotalGroupID equals payment.TotalGroupID
                      where paymentTransaction.TransferStatus == null || (paymentTransaction.TransferStatus != "01" && paymentTransaction.TransferStatus != "02")
                      select new { paymentTransaction, payment });

        var cartResult = from paymentTransaction in db.PaymentTransactions
                         from payment in db.Payments
                         where paymentTransaction.PaymentID == payment.ID || paymentTransaction.TotalGroupID == payment.TotalGroupID
                         where paymentTransaction.TransferStatus == null || (paymentTransaction.TransferStatus != "01" && paymentTransaction.TransferStatus != "02")
                         select new { paymentTransaction, payment };

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

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