简体   繁体   English

在Asp.NET MVC中与Linq进行数组比较

[英]Array comparison with Linq in Asp.NET MVC

I am using linq in Asp.NET MVC project for database operations. 我在Asp.NET MVC项目中使用linq进行数据库操作。 I want to attract customers who have sitecode in the AgentHealth table. 我想吸引在AgentHealth表中具有站点代码的客户。 How can I do this with linq in one operation? 如何在一次操作中使用linq做到这一点?

var CurrentCustomers = new List<Customer>();
var CurrentCustomersDisc = Database.Session
                                   .Query<agenthealts>()
                                   .ToList()
                                   .Select(x => x.sitecode)
                                   .Distinct()
                                   .ToList();

foreach(var CurrentDisc in CurrentCustomersDisc)
{
    var TempCustomer = Database.Session
                               .Query<Customer>()
                               .FirstOrDefault(x => x.deleted_at == null 
                                                 && x.SiteCode == CurrentDisc);
    if(TempCustomer != null)
    {
        CurrentCustomers.Add(TempCustomer);
    }               
}

You can select customers where their Id (or what the primary key is) in `CurrentCustomersDisc': 您可以在CurrentCustomersDisc中选择其ID(或主键是什么)的客户:

var CurrentCustomers = Database.Session.Query<Customer>.Where(c=>
     CurrentCustomersDisc.Select(cc=> cc.Id).Contains(c.Id));

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

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