嵌套的foreach循环到LINQ

时间:2018-05-18 11:31:35

标签: c# linq

是否可以将此代码重写为LINQ?我是linq的新手,似乎很难理解。

String ChildBelowList = "";

    if (!Childbelow.isEmpty()) {
        for (int iCB = 0; iCB < Childbelow.size(); iCB++) {

            ChildBelowList = ChildBelowList += Childbelow.get(iCB) + ",";



        }
        ChildBelowList = ChildBelowList.replaceAll("(^(\\s*?\\,+)+\\s?)|(^\\s+)|(\\s+$)|((\\s*?\\,+)+\\s?$)", "");

        tv_childbelow.setText(ChildBelowList);

    } else {
        ll_childbelow.setVisibility(View.GONE);
    }

3 个答案:

答案 0 :(得分:1)

由于条件EarningsList.Add()

,第一部分不易转换

但你可以很容易地重写最后2个。

假设<html> <head> <title>Page Title</title> <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script> </head> <body> <span class="outer"> <label class="lb">hi there</label> <input class="in" type="text"/> </span> <span class="outer"> <label class="lb">hi there</label> <input class="in" type="text"/> </span> <span class="outer"> <label class="lb">hi there</label> <input class="in" type="text"/> </span> </body> </html>完全符合其说法,您可以使用AddMoney()。否则,省略Sum()并在金额列表上运行单独的foreach。这样可以减少Linq。

Sum()

答案 1 :(得分:0)

不知道.AddMoney方法到底能做什么:

var query =
    from employee in EmployeeList
    let earnings = new Earnings(employee.Name, employee.LastName, employee.Bank, employee.Account)
    from item in ProductList
    from product in item.Products
    where product.EmployeeName == employee.Name
    where product.EmployeeLastName == employee.LastName
    group product.Count * product.Price by earnings;

List<Earnings> EarningsList =
    query
        .Aggregate(new List<Earnings>(), (a, x) =>
        {
            a.Add(x.Key);
            foreach (var y in x)
            {
                x.Key.AddMoney(y);
            }
            return a;
        });

如果.AddMoney只是算术地加钱,那么你可以这样做:

List<Earnings> EarningsList =
    query
        .Aggregate(new List<Earnings>(), (a, x) =>
        {
            a.Add(x.Key);
            x.Key.AddMoney(x.Sum());
            return a;
        });

只是一个小小的音符。您正在使用double来代表金钱。最好使用decimal,因为这有助于防止计算中的舍入错误。

答案 2 :(得分:0)

我认为如果你刚刚使用LINQ(没有foreach),这就是你想要的。这应该与IQueryable兼容,你真的不想在IQueryable上做foreach。

var newEarnings = from employee in EmployeeList
                              select new Earnings
                              {
                                  Name = employee.Name,
                                  LastName = employee.LastName,
                                  Bank = employee.Bank,
                                  Account = employee.Account,
                                  Money = (from daysData in ProductList
                                           from product in daysData.Products
                                           where employee.Name == product.EmployeeName && employee.LastName == product.EmployeeLastName
                                           select product).Sum(p => p.Count * p.Price)
                              };

            EarningsList = EarningsList.Union(newEarnings).ToList();

现在关于规范化。我猜你是为了在某种网格中展示它而制作了这样的POCO模型。你真的不应该让你的UI决定你的数据模型是什么样的。可能有其他原因要做到这一点,但它们与性能有关,我不认为你需要担心这只是喷气机。所以这是我对如何改变它的建议。

  1. 将Id属性添加到所有类。这总是一个好主意 不管你在做什么。这可以是随机字符串或自动字符串 增量,只是为了拥有一个独特的值,所以你可以玩这个 对象。
  2. 在您的课程中添加参考属性。不要复制值 员工对产品和收益。只需添加类型的属性 员工和/或添加EmployeeId属性
  3. 所以你的POCO看起来应该是这样的

    public class Employee
    {
        //It can be a Guid, string or what ever. I am not nesseserly recomending using Guids and you should look into this a bit more   
        public Guid Id { get; set; }
    
        public string Name { get; set; }
    
        public string LastName { get; set; }
    
        public string Bank { get; set; }
    
        public string Account { get; set; }
    }
    
    public class DaysData
    {
        public Guid Id { get; set; }
    
        public IEnumerable<Product> Products { get; set; }
    }
    
    public class Product
    {
        public Guid Id { get; set; }
    
        public Guid EmployeeId { get; set; }
    
        public Employee Employee { get; set; }
    
        public double Count { get; set; }
    
        public double Price { get; set; }
    }
    
    public class Earnings
    {
        public Guid Id { get; set; }
    
        public Guid EmployeeId { get; set; }
    
        public Employee Employee { get; set; }
    
        public double Money { get; set; }
    }
    

    和查询

    var newEarnings = from employee in EmployeeList
                              select new Earnings
                              {
                                  Id = Guid.NewGuid(),
                                  EmployeeId = employee.Id,
                                  Employee = employee,
                                  Money = (from daysData in ProductList
                                           from product in daysData.Products
                                           where employee.Id == product.EmployeeId
                                           select product).Sum(p => p.Count * p.Price)
                              };
    

    一旦你尝试实现数据持久性,无论你使用EF,Mongo,Elsatic,AzureTables还是其他任何东西,即使是简单的文件保存,这对你也会有很大的帮助。

相关问题