Home > Software engineering >  Drop down list in ASP MVC C# LiNQ
Drop down list in ASP MVC C# LiNQ

Time:09-12

This code shows a list of customer names in a drop down that have orders that are marked as samples. However, as there are multiple orders for the same customer, the list shows the same customer name serval times. How do I adjust the code to group by CustomerID please?

protected void FetchCustomerList()
        {
            ViewData["Customers"] = (from c in _stockEntities.tblAddresses
                                     join orders in _stockEntities.tblOrders on c.AddressID equals  orders.CustomerID
                                     where orders.Sample != 0
                                     orderby c.AName
                                     select c);
    }

CodePudding user response:

Why you join if you just want addresses with orders? You can use:

var customersWithOrders = _stockEntities.tblAddresses
    .Where(c => _stockEntities.tblOrders
        .Any(o => c.AddressID == o.CustomerID && o.Sample != 0))
    .OrderBy(c => c.AName);
                        

I guess that will be translated to an efficient EXISTS query.

  •  Tags:  
  • c#
  • Related