I'm trying to filter with LINQ a list of this type List<Dictionary<String, Object>>
but I can't find the way.
I have this code:
private List<Dictionary<String, Object>> ValidateScenarioProductItemData(List<Dictionary<String, Object>> pList)
{
foreach (var tItem in pList)
{
if (!string.IsNullOrEmpty(tItem["IsInternal"].ToString()))
{
var i = 0;
foreach (var item in tPeriods)
{
i++;
var tHasSails = tCycleProductItemSales.Where(
CPIS => CPIS.CycleId == Convert.ToInt32(tItem["CycleId"].ToString()) &&
CPIS.ProductItemId == Convert.ToInt32(tItem["ProductItemId"].ToString()) &&
CPIS.PeriodId == Convert.ToInt32(item.Id.ToString()));
if (tHasSails.Count() == 0)
{
tItem[string.Format("Datasource{0}Id", i)] = 0;
}
}
}
}
return pList;
}
I want to filter pList
in the foreach to avoid by the Key IsInternal
to avoid asking the next if and to avoid unnecessary calls.
I have tried this but it does not work: Filtering out values from a C# Generic Dictionary
答案 0 :(得分:1)
如果我说得对,你需要过滤掉键是“IsInternal”且对象不为空的字典项,那么这个怎么样 -
var filteredList = pList.Where(d => d.Keys.Contains("IsInternal") && d.Values != null);
答案 1 :(得分:0)
当您遍历2个数据源时,我认为您无法将其加入1个LINQ。 您可以做的最好的事情是将第一个条件加入.Where子句
private List<Dictionary<String, Object>> ValidateScenarioProductItemData(List<Dictionary<String, Object>> pList)
{
var tPeriods = new List<dynamic>();
var tCycleProductItemSales = new List<dynamic>();
foreach (var tItem in pList.Where(x=>!string.IsNullOrEmpty(x["IsInternal"].ToString())))
{
var i = 0;
foreach (var item in tPeriods)
{
i++;
var tHasSails = tCycleProductItemSales.Where(CPIS => CPIS.CycleId == Convert.ToInt32(tItem["CycleId"].ToString()) && CPIS.ProductItemId == Convert.ToInt32(tItem["ProductItemId"].ToString()) && CPIS.PeriodId == Convert.ToInt32(item.Id.ToString()));
if (!tHasSails.Any())
{
tItem[string.Format("Datasource{0}Id", i)] = 0;
}
}
}
return pList;
}