I was hoping I could get some help optimizing the following data retrieval. Here is the use case. I want to display a list of translators (suppliers) in a Telerik ASP.NET MVC grid. These translators have rates (or pricing schemes) by language pair. There are less 400 translators in this database. I want to display all of them initially, but let users filter by languages they translate. There is a supplier (translator) table, language pair table (with FK for source and target language), and language table.
Here is what I have but it is slow. The primary reason is that for each supplier, I need to get every unique language they translate (source and target language). I don’t know how to do that without a ForEach. I’m not even sure how I could do this in SQL without a while, temp table, or a cursor.
public List<tblSupplier> GetApprovedSuppliers()
{
var query = from s in db.tblSuppliers
join c in db.tblCountryLists on s.SupplierCountry equals c.CountryID into g1
from c in g1.DefaultIfEmpty()
select new
{
SupplierID = s.SupplierID,
SupplierName = s.CompanyName == null ? s.SupplierFirstName + " " + s.SupplierLastName : s.CompanyName,
SupplierEmails = s.SupplierEmails,
SupplierType = s.SupplierType,
Country = c.Countryname
};
List<tblSupplier> list = query.ToList().ConvertAll(s => new tblSupplier
{
SupplierID = s.SupplierID,
SupplierName = s.SupplierName,
SupplierEmails = s.SupplierEmails,
SupplierType = s.SupplierType,
Country = s.Country
}).OrderBy(s => s.SupplierName).ToList();
list.ForEach(s => s.Languages = this.GetLanguages(s.SupplierID));
return list;
}
public string GetLanguages(int supplierID)
{
var query = (from ps in db.tblSupplierPricingSchemes
join lp in db.tblLangPairs on ps.PSLangPairID equals lp.ProductID
join sl in db.tblLanguages on lp.SourceLanguageID equals sl.LanguageID
where ps.SupplierID == supplierID
select sl.LanguageDesc)
.Union
(from ps in db.tblSupplierPricingSchemes
join lp in db.tblLangPairs on ps.PSLangPairID equals lp.ProductID
join tl in db.tblLanguages on lp.TargetLanguageID equals tl.LanguageID
where ps.SupplierID == supplierID
select tl.LanguageDesc);
return string.Join(", ", query);
}
Any help is appreciated.
Thanks,
Steve
You must reduce number of queries. If you have correctly defined model you can start by using Include. If you don’t have correctly defined model with navigation properties you can replace multiple calls of
GetLanguagesto single call and reconstruct dataset in your application. To replace multiple calls of GetLanguages change message signature to:and replace
ps.SupplierID == supplierIDwith supplierIds.Contains(ps.SupplierID)` – you need at least EFv4 to make this work. So the result should look like:No you must only use this method like: