【问题标题】:C# linq IQueryable multiple filteringC# linq IQueryable 多重过滤
【发布时间】:2020-07-10 17:40:40
【问题描述】:

我找不到使用 LINQ 进行多重过滤的方法。

我想做什么:

1.过滤版本(已实现)

2.过滤CompanyName(过滤/删除重复)

3.获取最新时间戳

4.然后将所有内容添加到列表中。

到目前为止,这是我编写的代码(不起作用)。

public List<ReleaseStatistics> GetReleaseStatistics(IQueryable<InstallationInformation> input, IQueryable<DBContext.Version> mapping)
        {
            List<ReleaseStatistics> Releasestats = new List<ReleaseStatistics>();            
            foreach (var version in mapping)
            {
                IQueryable<InstallationInformation> QueryResult1 = input.Where(x => x.ProductVersion == version.VersionNumber);
 
                IQueryable<InstallationInformation> QueryResult2 = QueryResult1.GroupBy(x => x.CompanyName).SelectMany(y => y);

                List<InstallationInformation> ListofInstallationInformation = QueryResult2.ToList<InstallationInformation>();


                if (ListofInstallationInformation.Count >= 1)
                {
                    Releasestats.Add(new ReleaseStatistics
                    {
                        ReleaseVersion = version.ReleaseName,
                        CustomerCount = QueryResult1.Where(x => x.ProductVersion == version.VersionNumber).Count()
                    });
                }
            }  

            return Releasestats;
        }

补充资料:

其中一个问题是存在重复项,我想过滤/删除它们,但我想获取每个 CompanyName 的最新时间戳,然后将其添加到列表中。

【问题讨论】:

  • 所以QueryResult2 被复制了?您可以使用 MoreLinq 中的 DistinctBy 来删除这些重复项。
  • 但问题是重复的公司名称有自己独特的时间戳。所以我只需要为每个公司提供 1 个 Duplicate,它来自所有 Duplicate the latest Timestamp。也可能有多个公司:例如 Comanyname = C1 重复计数为 5,但每个重复都有一个唯一的时间戳
  • @johnny 您可能会提供更多详细信息您的输入/所需输出/不需要的输出,以确保我们的想法正确。

标签: c# linq


【解决方案1】:

问题在于这条线

IQueryable<InstallationInformation> QueryResult2 = QueryResult1.GroupBy(x => x.CompanyName).SelectMany(y => y);

实际上什么都不做。

假设 QueryResult1 是

公司名称 | F1 | F2 |

CN1 | f1a | f2a |

CN1 | f1a | f2a |

CN2 | f1b | f2b |

那么 QueryResult1.GroupBy(x => x.CompanyName) 是

集团 |数据


CN1 |公司名称 | F1 | F2 |

     CN1          |  f1a | f2a |

     CN1          |  f1a | f2a |

CN2 |公司名称 | F1 | F2 |

     CN2          |  f1b | f2b |

然后 QueryResult1.GroupBy(x => x.CompanyName).SelectMany(y => y);又来了

公司名称 | F1 | F2 |

CN1 | f1a | f2a |

CN1 | f1a | f2a |

CN2 | f1b | f2b |

你想做的大概是

var QueryResult2 = QueryResult1.GroupBy(x => x.CompanyName).Select(y => new {CompanyName = y.Key, MaxTimestamp = y.Max(z => z.TimeStamp)});

【讨论】:

  • System.Linq.IQueryable>" 无法隐式转换为“”。已经存在显式转换(可能是转换不见了)。
  • 是的.. 因为它返回动态的 IEnumerable。我编辑了代码:只需使用 var QueryResult2 而不是 IQueryable QueryResult2
【解决方案2】:

我用类来模拟你的查询

           DBContext context = new DBContext();
            List<InstallationInformation> input = new List<InstallationInformation>();


            var query = (from m in context.mapping
                         join  i in input on  m.VersionNumber equals i.ProductVersion
                         select new { version = m.VersionNumber, companyName = i.CompanyName}
                        ).ToList();

            List<ReleaseStatistics> results = query.GroupBy(x => x.version).Select(x =>  new ReleaseStatistics() { ReleaseVersion = x.Key, CustomerCount = x.Distinct().Count() }).ToList();        

        }
    }
    public class DBContext
    {
        public List<Version> mapping { get; set; }
    }
    public class InstallationInformation
    {
        public int ProductVersion { get; set; }
        public string CompanyName { get; set; }
    }

    public class Version
    {
       public int VersionNumber { get; set; }
    }
    public class ReleaseStatistics
    {
        public int ReleaseVersion { get; set; }
        public int CustomerCount { get; set; }
    }

【讨论】:

  • 我需要更多信息。你在查询中得到什么?我怀疑 VersionNumber 和 ProductVersion 不同,所以连接失败。
【解决方案3】:

这是我的最终结果:

public List<ReleaseStatistics> GetReleaseStatistics(IQueryable<InstallationInformation> input, IQueryable<DBContext.Version> mapping)
        {
            List<ReleaseStatistics> Releasestats = new List<ReleaseStatistics>();            
            foreach (var version in mapping)
            {
                IQueryable<InstallationInformation> QueryResult1 = input.Where(x => x.ProductVersion == version.VersionNumber);

                IQueryable<string> companynamecollection = QueryResult1.Select(x => x.CompanyName).Distinct();

                List<InstallationInformation> listofAggregatedInstallationInformation = new List<InstallationInformation>();

                foreach (var item in companynamecollection)
                {
                    var maxdatetime = QueryResult1.Where(x => x.CompanyName == item).Select(x => x.Timestamp).Max();
                    IQueryable<InstallationInformation> listofresult = QueryResult1.Where(y => y.CompanyName == item && y.Timestamp == maxdatetime);

                    foreach (var item2 in listofresult)
                    {
                        listofAggregatedInstallationInformation.Add(new InstallationInformation
                        {
                            InstallationInformationID = item2.InstallationInformationID,
                            LicenceKey = item2.LicenceKey,
                            ProductName = item2.ProductName,
                            ProductVersion = item2.ProductVersion,
                            CompanyName = item2.CompanyName,
                            Timestamp = item2.Timestamp,
                            ImportRunID = item2.ImportRunID
                        });
                    }
                }

                if (listofAggregatedInstallationInformation.Count >= 1)
                {
                    Releasestats.Add(new ReleaseStatistics
                    {
                        ReleaseVersion = version.ReleaseName,
                        CustomerCount = listofAggregatedInstallationInformation.Where(x => x.ProductVersion == version.VersionNumber).Count()
                    });
                }
            }  

            return Releasestats;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-26
    • 2011-02-09
    • 1970-01-01
    • 2020-11-12
    相关资源
    最近更新 更多