在我的实体框架查询中使用太多的foreach循环?

时间:2012-10-08 21:23:17

标签: entity-framework c#-4.0

我正在尝试学习实体框架。我很高兴我从查询返回的结果给了我需要的数据,但我发现我正在使用for-each循环和单独的数据库请求来获取我需要的信息。

如果有人可以查看我的代码并建议我是否有办法让事情更简洁,并且如果我没有真正获得实体框架提供的功能,我将不胜感激(我想我在这里错过了重点)某处!!!)。这里的任何方式都是问题和代码(我希望有意义):

尝试从我的数据库中的4个表进行查询。一个是无关的(由于我的懒惰 - 我需要修改它,但它不是目前的交易破坏者)。其他3个与1对多关系有关。这是一个总结:

UserClients - 不相关的表(应该链接到LeadClient但不重要)

[LeadClient PK = GroupCode]可以[问题FK = LeadCode] [问题PK = MatterNumber]一到多[发票FK =物质]

我正在尝试创建一个DTO对象列表来填充GridView。我的DTO如下:

public class dto
{
    public string clientCode { get; set; }
    public string clientName { get; set; }
    public decimal totalDebt { get; set; }


}

我将一个statementName变量传递给一个方法,然后我根据UserClients查询以获取客户端代码列表。

这是填充List对象的方法:

      public static List<dto> StateNameLeadCodes(string stateName)
    {
        //DTO objects
        List<dto> items = new List<dto>();

        //Leadcode list for statement name
        List<string> leadcodes = (from o in repository.UserClients where o.StatementName == stateName select o.LeadCode).ToList();

        //From leadcodes populate items list
       foreach(string s in leadcodes)
       {
           //Get clientCode and matter list for the client code
           var code = (from o in repository.LeadClients where o.GroupCode == s select new { o.ClientName, o.Matters}).First();


           dto temp = new dto();

           temp.clientCode = s;

           temp.clientName = code.ClientName;


           //Get a list if invoices for each matter 
           foreach (Matter m in code.Matters)
           {
               List<Invoice> invoices = (from o in repository.Invoices where o.Matter == m.Matternumber select o).ToList();
               //Calculate total debt for all invoices
               foreach (Invoice inv in invoices)
               {
                   temp.totalDebt += (inv.debt_Fee + inv.debt_Costs + inv.debt_VAT);
               }

           }

                items.Add(temp);
            }


        return items;
         }

当我想将单个项目和列表的组合返回给DTO时,我发现自己做了类似的事情。我看到我的Entity类有一个EntityCollection用于One to Many关系。我试图选择实体对象作为我的select语句的一部分,但我得到的错误是EnitityCollection无法转换为List&lt;&gt;。

如何清理此代码的任何提示和指示都会很棒 - 提前感谢!!

按要求添加了Entity对象的图像:

EntityImage

1 个答案:

答案 0 :(得分:1)

我不确定我是否正确阅读并理解了一切( 很难为我阅读:)),但你可以试试这个解决方案:

public static List<dto> StateNameLeadCodes(string stateName)
{
    return (from uc in repository.UserClients
            join lc in repository.LeadClients
                on uc.LeadCode equals lc.GroupCode
            where uc.StatementName == stateName
            select new dto
            {
                clientCode = uc.LeadCode,
                clientName = lc.ClientName,
                totalDebt = lc.Matters
                    .Sum(m => m.Invoices
                        .Sum(i => i.debt_Fee + i.debt_Costs + i.debt_VAT))
            }).ToList();
}

如果LeadClient的Matter集合或Matter的Invoices集合为空,则可能会出现问题,因为EF无法将空集合的总和实现为decimal值。可能需要使用类似

的内容
totalDebt = lc.Matters
    .Sum(m => m.Invoices
        .Sum(i => (decimal?)(i.debt_Fee + i.debt_Costs + i.debt_VAT)) ?? 0m)