【问题标题】:Select rows with LINQ from List<Dictionary<string, object>>从 List<Dictionary<string, object>> 中选择带有 LINQ 的行
【发布时间】:2015-02-12 07:01:39
【问题描述】:

让我们假设简单的数据:

Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("obj 1", null);
dict.Add("obj 2", new object());
dict.Add("obj 3", new object());
dict.Add("obj 4", null);

Dictionary<string, object> dict2 = new Dictionary<string, object>();
dict2.Add("obj 1", null);
dict2.Add("obj 2", new object());
dict2.Add("obj 3", null);
dict2.Add("obj 4", null);

Dictionary<string, object> dict3 = new Dictionary<string, object>();
dict3.Add("obj 1", null);
dict3.Add("obj 2", null);
dict3.Add("obj 3", new object());
dict3.Add("obj 4", null);

List<Dictionary<string, object>> list = new List<Dictionary<string, object>> {dict, dict2, dict3};

是否可以选择行并删除所有列中为空的键/值?因此,在我的列表中之后将是:

dict({"obj 2", object}, {"obj 3", object})
dict2({"obj 2", object}, {"obj 3", null})
dict2({"obj 2", null}, {"obj 3", object})

【问题讨论】:

标签: c# linq linq-to-objects


【解决方案1】:

试试这个:

 list.ForEach(x =>
        {

            x.Where(y => y.Value == null).ToList().ForEach(z=>
                {
                    if(list.All(l=>!l.ContainsKey(z.Key)|| l[z.Key]==null))
                        x.Remove(z.Key);
                });
        });

【讨论】:

  • 我愿意,但我还需要 2 个声望 .. 当我达到它时,我会这样做
猜你喜欢
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多