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列)是最大/最新日期
例如,如果第二个列表中的数据
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);
}