簡體   English   中英

比較c#中不同類型的列表

[英]compare different type of list in c#

我有兩個列表一個是字符串列表,另一個是一個類的列表。該類包含一些屬性,如soid,itemname,qty等,它的值也..字符串列表和類列表有一些共同的屬性。所以我想檢查帶有類列表的字符串列表,並將常用值添加到一個字典中。

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");

所以我想比較類成員名稱和字符串列表名稱

不確定我是否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)));

創建一個看起來像的字典

在此輸入圖像描述

編輯:

如果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)));

如果我理解正確,你想比較類型本身,而不是列表中的實例。

你可以這樣做:

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

所以Input有4個屬性中的3個,而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();

這是我的意見:

在此輸入圖像描述

只需注意,使用GetValue時要小心:例如,您必須對其進行細微更改才能使用Indexers。

使用此ReflectionExt.GetAttr,您可以執行以下操作:

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

   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM