【问题标题】:how to Filter one list based on another lists field如何根据另一个列表字段过滤一个列表
【发布时间】:2016-01-03 18:49:14
【问题描述】:

我正在从单独的表中从数据库中获取数据并存储在单独的变量中。

这是第一个列表

   var res = (from v in context.MasterValues
                       join c in context.MasterCategories on v.Category_Id equals c.Id
                       where c.Model_Key == (int)model && c.Name == tableName && v.Version_Id == version && v.Active == "Y" && c.Name.Equals("FXRates")
                       select new 
                       {
                           Id = v.Id,
                           VersionId = v.Version_Id,
                           Text1 = v.Text1,
                       }).ToList();

这是第二个列表

        var fxview = context.MasterFXRates.Select
            (x => new
            {
                Currency_code = x.Currency_code,
                Rate = x.Rate,
                Effective_dt = x.Effective_dt
            }
            ).ToList();

那么现在如何根据我的第一个列表中的数据过滤我的第二个列表 fxview 中的数据? IE。 我需要过滤 list2 的 Currency_code 数据与 List1 的 Text1 匹配的数据,其中有效 dt(日期时间列)为最大/最新日期

例如,如果第二个列表数据有

  1. ABC , 100 , 2010-10-10
  2. ABC , 120 , 2014-12-12
  3. DEF ,700 , 2013-08-02
  4. DEF ,500 ,2015-06-06

List 1(res) 有以下数据

  1. 1 , 1 , ABC
  2. 2、1、DEF

所以过滤后我的最终列表必须有以下输出

  1. ABC ,120 (由于 2014-12-12 是最晚的日期,因此获取对应的值并过滤重复值 (ABC,100)。)

2.DEF ,500(由于2015-06-06是最晚的日期,所以取对应的值,过滤掉重复值(DEF,&00)。)

【问题讨论】:

  • 如果列表只需要过滤,我建议改为JOIN

标签: c# entity-framework linq linq-to-entities


【解决方案1】:
 var result = from masterFxRate in masterFxRates
                     join masterValue in masterValues on masterFxRate.Currency_code equals masterValue.Text1
                     group masterFxRate by
                     new
                     {
                         masterFxRate.Currency_code
                     } into groupedRates
                     select new
                     {
                         groupedRates.Key.Currency_code,
                         Rate = groupedRates.FirstOrDefault(g => g.Effective_dt != null 
                                                              && g.Effective_dt == groupedRates.Max(c => c.Effective_dt)).Rate
                     };

        foreach (var item in result)
        {
            Console.WriteLine("{0} : {1} ", item.Currency_code, item.Rate);
        }

【讨论】:

    【解决方案2】:
      fxView = fxView.OrderByDescending(x => x.effectiveDate).ToList();
      var result = new List();
     res.ForEach((x) => result.Add(fxView.First(y => x.text1 == y.currency_code)));
    

    如果有效日期已经是日期时间,这应该可以工作,否则将其转换为日期时间

     var fxview = context.MasterFXRates.Select
            (x => new
            {
                Currency_code = x.Currency_code,
                Rate = x.Rate,
                Effective_dt = Convert.ToDateTime(x.Effective_dt)
            }
            ).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      相关资源
      最近更新 更多