Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何基于另一个列表字段筛选一个列表_C#_Entity Framework_Linq_Linq To Entities - Fatal编程技术网

C# 如何基于另一个列表字段筛选一个列表

C# 如何基于另一个列表字段筛选一个列表,c#,entity-framework,linq,linq-to-entities,C#,Entity Framework,Linq,Linq To Entities,我从数据库中的分隔表中提取数据,并存储在单独的变量中 这是第一个列表 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 == tableNa

我从数据库中的分隔表中提取数据,并存储在单独的变量中

这是第一个列表

   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();
那么现在如何根据第一个列表中的数据过滤第二个列表中的数据呢? 即 我需要过滤列表2的货币代码数据与列表1的文本1匹配的数据,其中有效日期(datetime列)是最大/最新日期

例如,如果第二个列表中的数据

  • ABC,1002010-10-10
  • 美国广播公司,1202014-12-12
  • DEF,7002013-08-02
  • DEF,5002015-06-06
  • 清单1(res)有以下数据

  • 1,1,ABC
  • 2,1,DEF
  • 因此,在筛选后,我的最终列表必须具有以下输出

  • ABC,120(由于2014-12-12是最晚的日期,因此将获取相应的值,并应过滤重复值(ABC,100))
  • 2.DEF,500(由于2015-06-06是最新日期,因此将获取相应的值,并应过滤重复值(DEF,&00)

    如果effectiveDate已经是DateTime,则应该可以使用,否则将其转换为DateTime

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

    如果列表只需要执行过滤,我建议改为执行
    JOIN
     var fxview = context.MasterFXRates.Select
            (x => new
            {
                Currency_code = x.Currency_code,
                Rate = x.Rate,
                Effective_dt = Convert.ToDateTime(x.Effective_dt)
            }
            ).ToList();
    
     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);
            }