İsmail Kasap
İsmail Kasap

Reputation: 109

Array comparison with Linq in Asp.NET MVC

I am using linq in Asp.NET MVC project for database operations. I want to attract customers who have sitecode in the AgentHealth table. How can I do this with linq in one operation?

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

Upvotes: 0

Views: 365

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

You can select customers where their Id (or what the primary key is) in `CurrentCustomersDisc':

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

Upvotes: 1

Related Questions