从一本字典中选择第二本字典

时间:2019-01-06 22:51:35

标签: c# linq dictionary

我有2个这样的字典List<Dictionary<string, object>>

[{"Id":"2","FirstName":null,"MiddleName":null,"LastName":null,"Birthday":null,"Username":null,"Password":null,"LastLogin":null,"CreateDateTime":null,"UpdateDateTime":null,"Blocked":null,"HasOrders":null}]

并有一个Dictionary<string, string>这样的字典作为过滤器

var dict = new Dictionary<string, object>
        {
            { "Username", "myUsername" },
            { "Password", "myPassword" }
        };

我的问题是,如何在第二个列表的帮助下过滤掉第一个列表中的记录。 请注意,我需要在第二个列表中包含2或3个动态键/值的where语句。这些JSON结构中有很多记录。

如果有人能指出我正确的方向,那就太好了。

-更新-

类似我想要的东西,但是没有foreach:

 foreach(Dictionary<string, object> v in Data)
                                {
                                    v.Where(x => 
 x.Key.Contains(nList.Where(n => n.Key == x.Key && n.Value = x.Value)));
                                }

我不知道如何在LINQ中做到这一点

1 个答案:

答案 0 :(得分:0)

我建议您分两步执行此操作。显然,第一步是将List>结构转换为List。将其作为“词典收藏”来处理不是一个好方法。

    public class UserPass
    {
        public string Username{get;set;}
        public string Password{get;set;}
    }

    public class User
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public DateTime? Birthday { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public DateTime? LastLogin { get; set; }
        public DateTime? CreateDateTime { get; set; }
        public DateTime? UpdateDateTime { get; set; }
        public bool? Blocked { get; set; }
        public bool? HasOrders { get; set; }
    }

例如,

var dictionary1 = new Dictionary<string,object>()
    {
        ["Id"]="2",
        ["FirstName"]=null,
        ["MiddleName"]=null,
        ["LastName"]=null,
        ["Birthday"]=null,
        ["Username"]="anuviswan",
        ["Password"]="password",
        ["LastLogin"]=null,
        ["CreateDateTime"]=null,
        ["UpdateDateTime"]=null,
        ["Blocked"]=null,
        ["HasOrders"]=null
    };

    var dictionary2 = new Dictionary<string,object>()
    {
        ["Id"]="3",
        ["FirstName"]=null,
        ["MiddleName"]=null,
        ["LastName"]=null,
        ["Birthday"]=null,
        ["Username"]="user1",
        ["Password"]=null,
        ["LastLogin"]=null,
        ["CreateDateTime"]=null,
        ["UpdateDateTime"]=null,
        ["Blocked"]=null,
        ["HasOrders"]=null
    };

    var dataList= new List<Dictionary<string,object>>
    {
        dictionary1,dictionary2
    };

    var dict1 = new Dictionary<string, object>
        {
            { "Username", "myUsername" },
            { "Password", "myPassword" }
        };

    var dict2 = new Dictionary<string, object>
        {
            { "Username", "anuviswan" },
            { "Password", "password" }
        };

    var filterList = new List<Dictionary<string,object>> {dict1, dict2};

    var dataObjectList = dataList.Select(x=> new User
    {
        Id = Convert.ToInt32(x[nameof(User.Id)]),
        Username = Convert.ToString(x[nameof(User.Username)]),
        Password = Convert.ToString(x[nameof(User.Password)]),
    });

    var filterObjectList = filterList.Select(x=>
    new UserPass
    {
        Username = Convert.ToString(x[nameof(UserPass.Username)]),
        Password = Convert.ToString(x[nameof(UserPass.Password)]),
    });

有了新的列表,您可以查询为

var result =dataObjectList.Join(filterObjectList, 
                           d=>d.Username, 
                           f=>f.Username,
                           (x,y)=> new 
                              {
                                Data = x, 
                                Filter=y
                              })
                          .Where(x=>x.Data.Username.Equals(x.Filter.Username) 
                             && x.Data.Password.Equals(x.Filter.Password))
                         .Select(x=>x.Data);;