Reputation: 109
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
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