-2

I have 2 dictionaries List<Dictionary<string, object>> like this:

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

and have a Dictionary like this Dictionary<string, string> for filter

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

My question is how I can filter out within the first List the records with help of the second list. Please note that I need a where statement with 2 or 3 dynamic keys/values in the second list. There are a lot of records in these JSON structures.

It be great if somebody can point me to the right direction.

--UPDATE--

something like as I want to have, but than without the 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)));
                                }

I have no idea on how todo this in LINQ

  • [This](https://stackoverflow.com/questions/6359980/proper-linq-where-clauses) post will answer your question. Use Linq where clause. – rCgLT Jan 06 '19 at 22:55
  • "how I can filter out within the first List the records with help of the second list"? it would be clearer if you specified what you expect as a result after the filter is done. – Ousmane D. Jan 06 '19 at 22:57
  • 1
    It would be awesome if you could provide a [mcve] with sample inputs and expected results based on those sample inputs. – mjwills Jan 06 '19 at 22:59
  • I need to filter in this case if a user exist in this dictionary by username and password – user10876501 Jan 06 '19 at 23:02
  • as @mjwills has already mentioned. _provide a Minimal, Complete, and Verifiable example with sample inputs and expected results._ – Ousmane D. Jan 06 '19 at 23:04
  • Yep - as @Aomine says we don't need the solution, but we **do** need the inputs and expected results. And it needs to be written in C# so we can copy and paste into a console app and run it. – mjwills Jan 06 '19 at 23:10

1 Answers1

0

I suggest you do this in couple of steps. First step, obviously would be to convert the List> structure to a List. Handling it as Collection of Dictionary is not a good approach.

    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; }
    }

For example,

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)]),
    });

With the new Lists in place, you can query as

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);;
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • The problem is I get this data in a Dictionairy in already try to push this into a dynamic class and on runtime use generated classes but also not really works. Beside the filter parameters are changing the whole time so I cant use static names. This is a nice approach and would work if I could push the Dictionary in a dynamic object – user10876501 Jan 07 '19 at 08:29
  • When you say Filter parameters change, so could there be a case where there are no username password? That can never happen right? – Anu Viswan Jan 07 '19 at 08:32
  • yes it can, I am looking for an solution where I can inherited the filter data as something like Data.Select(x => x.Where(foreach n in filter { key = value })). I know this is impossible but something this direction I am looking. I am not even sure if its possible what I want within this aspect, however I know these filter should be fast as some of these queries will go through 1000th of records – user10876501 Jan 07 '19 at 08:45
  • I just found something what might do the trick. https://msdn.microsoft.com/en-us/vstudio/bb894665.aspx – user10876501 Jan 07 '19 at 08:57