Compare different types of lists in C #

I have two lists: this is a list of strings, and the other is a list of one class. This class contains some properties, such as soid, itemname, qty, etc., as well as its value. The list of strings and the list of classes have some common property .so I wanted to check the list of strings with the list of classes and add common values ​​to one dictionary.

public class Input
   {
       public string soid { get; set; }

       public string itemname { get; set; }

       public int qty { get; set; }
   }
list<Input> inputclass=new  list<Input>();//inputclass list

List<string> metadata=new List<string>();//string list
metadata.Add("itemname");
        metadata.Add("soid");
        metadata.Add("qty");

so I wanted to compare the name of a class member with the name of a list of strings

+1
source share
3 answers

If I understand correctly, you want to compare the type itself, not the instances inside this list.

You can do it as follows:

List<string> metadata = new List<string>();//string list
metadata.Add("itemname");
metadata.Add("soid");
metadata.Add("qty");
metadata.Add("yada");

var result = from str in metadata
             join prop in typeof(Input).GetProperties() on str equals prop.Name
             select str;

foreach (string prop in result)
{
    Console.WriteLine(prop);
}

, T , , .

: when we get the common name between list<input> and string how will get the value of corresponding member of the class.now you return only common names r8..?

. , :

public class Input
{
    public string soid { get; set; }

    public string itemname { get; set; }

    public int qty { get; set; }
}

public class Yada : Input
{
    public string yada { get; set; }
}

So Input 3 4 , Yada 4.

, :

List<Input> inputclass = new List<Input>();//inputclass list

inputclass.Add(new Input() { itemname = "test",soid="myId",qty=10 });
inputclass.Add(new Yada() { itemname = "test2",soid="myId2", yada = "woo",qty=20 });

, , :

var result = inputclass.Select(
                 input => (from str in metadata
                           join prop in input.GetType().GetProperties()
                           on str equals prop.Name
                           select new { Obj = input, Prop = str, Value = prop.GetValue(input, null) }))
                           .SelectMany(i => i)
                           .GroupBy(obj => obj.Obj);

foreach (var obj in result)
{
    Console.WriteLine(obj.Key);
    foreach (var prop in obj)
    {
        Console.WriteLine(prop.Prop + ":" + prop.Value);
    }
    Console.WriteLine();
 }

 Console.ReadKey();

:

enter image description here

, GetValue: , , .

+1

, 100%, :

var inputclass= new List<Input>() {
    new Input(){ soid="123", itemname="Bar", qty=123 },
    new Input(){ soid="777", itemname="Foo", qty=999 } 
};
List<string> metadata=new List<string>() { "itemname", "soid", "qty" };

.ToDictionary() .GetProperty(),

Type t = typeof(Input);
var result = metadata.ToDictionary(i => i, i => inputclass.Select(c => t.GetProperty(i).GetValue(c)));

,

enter image description here

EDIT:

metadata , Input, :

var result = metadata.Select(i => t.GetProperty(i))
                     .Where(i => i != null)
                     .ToDictionary(i => i.Name, i => inputclass.Select(c => i.GetValue(c)));
+3

Using this ReflectionExt.GetAttr , you can do something like this:

var dict = new Dictionary<string, List<string>>();

foreach(var listItem in inputclass)
{
   foreach(var metaItem in metadata)
   {
       try
       {
            var value = ReflectionExt.GetAttr(listItem, metaItem);

            if (dict[metaItem] == null)
                dict[metaItem] = new List<string>();

            if (!dict[metaItem].Contains(value)) // Add only if not exists
                dict[metaItem].Add(value);
       }
       catch
       {
           ;
       }

   }

}
0
source

Source: https://habr.com/ru/post/1671579/


All Articles