Asp.net mvc 3 为ASP.NET MVC Telerik网格优化EF Linq到实体查询

Asp.net mvc 3 为ASP.NET MVC Telerik网格优化EF Linq到实体查询,asp.net-mvc-3,entity-framework,linq-to-entities,telerik-mvc,Asp.net Mvc 3,Entity Framework,Linq To Entities,Telerik Mvc,我希望我能得到一些帮助来优化下面的数据检索。下面是用例。我想在Telerik ASP.NET MVC网格中显示翻译人员(供应商)列表。这些翻译人员有按语言对划分的费率(或定价方案)。此数据库中的翻译人员少于400名。我想一开始就显示所有这些内容,但让用户根据他们翻译的语言进行过滤。有一个供应商(译者)表、语言对表(源语言和目标语言为FK)和语言表 这是我的东西,但速度很慢。主要原因是,对于每个供应商,我需要获得他们翻译的每一种独特语言(源语言和目标语言)。我不知道没有ForEach怎么做。我甚至

我希望我能得到一些帮助来优化下面的数据检索。下面是用例。我想在Telerik ASP.NET MVC网格中显示翻译人员(供应商)列表。这些翻译人员有按语言对划分的费率(或定价方案)。此数据库中的翻译人员少于400名。我想一开始就显示所有这些内容,但让用户根据他们翻译的语言进行过滤。有一个供应商(译者)表、语言对表(源语言和目标语言为FK)和语言表

这是我的东西,但速度很慢。主要原因是,对于每个供应商,我需要获得他们翻译的每一种独特语言(源语言和目标语言)。我不知道没有ForEach怎么做。我甚至不知道如何在SQL中不用一段时间、临时表或游标就可以做到这一点

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);
}
公共列表GetApprovedSuppliers()
{
var query=来自db.tblSuppliers中的s
将c加入s.SupplierCountry上的db.tblCountryList中等于c.CountryID加入g1
从g1.DefaultIfEmpty()中的c开始
选择新的
{
供应商ID=s.供应商ID,
SupplierName=s.CompanyName==null?s.SupplierFirstName+“”+s.SupplierLastName:s.CompanyName,
供应商电子邮件=s.供应商电子邮件,
供应商类型=s.SupplierType,
Country=c.Countryname
};
List List=query.ToList().ConvertAll(s=>新TBL供应商
{
供应商ID=s.供应商ID,
供应商名称=s.SupplierName,
供应商电子邮件=s.供应商电子邮件,
供应商类型=s.SupplierType,
国家=美国国家
}).OrderBy(s=>s.SupplierName).ToList();
list.ForEach(s=>s.Languages=this.GetLanguages(s.SupplierID));
退货清单;
}
公共字符串GetLanguages(int supplierID)
{
var query=(来自db.tblSupplierPricingSchemes中的ps
在ps.PSLangPairID上的db.tblLangPairs中加入lp等于lp.ProductID
在lp.SourceLanguageID等于sl.LanguageID的db.tbllinguages中加入sl
其中ps.SupplierID==SupplierID
选择sl.LanguageDesc)
.工会
(来自db.TBL供应商定价方案中的ps)
在ps.PSLangPairID上的db.tblLangPairs中加入lp等于lp.ProductID
在lp.TargetLanguageID等于tl.LanguageID的db.tbl语言中加入tl
其中ps.SupplierID==SupplierID
选择tl.LanguageDesc);
返回字符串。Join(“,”查询);
}
感谢您的帮助

谢谢,
Steve

您必须减少查询数量。如果您已经正确定义了模型,那么可以从使用Include开始。如果没有正确定义带有导航属性的模型,则可以将对
GetLanguages
的多个调用替换为单个调用,并在应用程序中重建数据集。要替换GetLanguages的多个调用,请将消息签名更改为:

public Dictionary<id, string> GetLanguages(int[] suppplierIds)
否您只能使用以下方法:

Dictionary<int, string> languages = GetLanguages(list.Select(s => s.SupplierID));
list.ForEach(s => s.Languages = languages[s.SupplierID]);
Dictionary languages=GetLanguages(list.Select(s=>s.SupplierID));
list.ForEach(s=>s.Languages=Languages[s.SupplierID]);

这看起来很棒,拉迪斯拉夫!今天下午晚些时候我要试试这个。谢谢
Dictionary<int, string> languages = GetLanguages(list.Select(s => s.SupplierID));
list.ForEach(s => s.Languages = languages[s.SupplierID]);