如何根据另一个列表字段过滤一个列表
how to Filter one list based on another lists field
我正在从分隔表中的数据库中获取数据并存储在单独的变量中。
这是第一个列表
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。
我需要过滤数据,其中 Currency_code 的 list2 数据与 List1 的 Text1 匹配,其中 effective_dt(日期时间列)是 maximum/Latest 日期
例如,如果第二个列表数据有
- 美国广播公司, 100, 2010-10-10
- 美国广播公司, 120, 2014-12-12
- DEF ,700 , 2013-08-02
- DEF ,500 ,2015-06-06
列表 1(res) 有以下数据
- 1 , 1 , ABC
- 2 , 1 , 防御
所以在过滤后我的最终列表必须有以下输出
- ABC ,120(因为2014-12-12是最新的日期,所以取对应的值,过滤掉重复值(ABC,100))
2.DEF ,500(因为2015-06-06是最新的日期,所以取对应的值,过滤重复值(DEF,&00)。)
fxView = fxView.OrderByDescending(x => x.effectiveDate).ToList();
var result = new List();
res.ForEach((x) => result.Add(fxView.First(y => x.text1 == y.currency_code)));
如果 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();
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);
}
我正在从分隔表中的数据库中获取数据并存储在单独的变量中。
这是第一个列表
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。 我需要过滤数据,其中 Currency_code 的 list2 数据与 List1 的 Text1 匹配,其中 effective_dt(日期时间列)是 maximum/Latest 日期
例如,如果第二个列表数据有
- 美国广播公司, 100, 2010-10-10
- 美国广播公司, 120, 2014-12-12
- DEF ,700 , 2013-08-02
- DEF ,500 ,2015-06-06
列表 1(res) 有以下数据
- 1 , 1 , ABC
- 2 , 1 , 防御
所以在过滤后我的最终列表必须有以下输出
- ABC ,120(因为2014-12-12是最新的日期,所以取对应的值,过滤掉重复值(ABC,100))
2.DEF ,500(因为2015-06-06是最新的日期,所以取对应的值,过滤重复值(DEF,&00)。)
fxView = fxView.OrderByDescending(x => x.effectiveDate).ToList();
var result = new List();
res.ForEach((x) => result.Add(fxView.First(y => x.text1 == y.currency_code)));
如果 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();
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);
}