从对象列表中获取唯一对象

时间:2015-05-26 05:28:19

标签: c# asp.net linq

我有这样的类对象:

    public class SD
    {
        public string s { get; set; }
    }

    public class Obj
    {
        public string p { get; set; }
        public string u { get; set; }
        public List<SD> sD { get; set; }
    }

    public class ObjAssignStudy
    {
        public List<Obj> obj { get; set; }
    }

我以这种方式得到这些数据:

{
  "obj":[
    {"p":"1","usertype":"A","studyData":[{"study":"1"},{"study":"2"}]},
    {"p":"2","usertype":"A","studyData":[{"study":"1"}]}
    {"p":"1","usertype":"A","studyData":[{"study":"2"},{"study":"3"}]}
  ]
}

我想要的是,我希望获得不同的P并将相应的s附加到它上,即。我希望final对象包含这样的数据:

{
  "obj":[
    {"p":"1","usertype":"A","studyData":[{"study":"1"},{"study":"2"},{"study":"3"}]},
    {"p":"2","usertype":"A","studyData":[{"study":"1"}]}
  ]
}

我能用c#或linq实现这个目标吗?

2 个答案:

答案 0 :(得分:1)

var objs = new List<Obj>(){
    new Obj 
    { 
        p = "1",
        u = "A",
        sD = new List<SD>() {new SD() { s = "1"}, new SD() { s = "2"}}
    },
    new Obj 
    { 
        p = "2",
        u = "A",
        sD = new List<SD>() {new SD() { s = "1"}}
    },
    new Obj 
    { 
        p = "1",
        u = "A",
        sD = new List<SD>() {new SD() { s = "2"}, new SD() { s = "3"}}
    }
};

var distinct = from obj in objs
               group obj by new { obj.p } into g
               select new Obj {
                   p = g.Key.p,
                   u = g.First().u, 
                   sD = g.SelectMany(i => i.sD).Distinct().ToList() 
               };

修改班级SD以使用Distinct

public class SD
{
    public string s { get; set; }

    public override bool Equals(object obj)
    {
        return string.Equals(s, (obj as SD).s);
    } 

    public override int GetHashCode()
    {
        return s.GetHashCode();
    }
}

答案 1 :(得分:0)

在此特定情况下,您可以使用indexer获取与您传递给w3schools的值相对应的特定List<SD>,请仔细阅读将详细说明的代码; < / p>

创建object

Indexer

在main函数中,您可以访问索引器并存储收集详细信息,如下所示:

public List<SD> this[string index]
{
   get
     {
       foreach (Obj item in ListObj)
        {
          if (item.p == index)
          return item.sD;
        }
        return new List<SD>();
     }              
 }

完整代码供您参考:

    static void Main(string[] args)
    {
        ObjAssignStudy newInstance = new ObjAssignStudy();
        List<SD> sampleData=newInstance["b"];// Gives the output
       //collection as : EmptyList1,EmptyList2
    }

测试案例:      List sampleData = newInstance [&#34; a&#34;]; //给出输出

  

EmptyList1

 class Program
    {
        static void Main(string[] args)
        {
            ObjAssignStudy newInstance = new ObjAssignStudy();
         List<SD> sampleData=newInstance["c"];
        }
        public class SD
        {
            public string s { get; set; }
        }

        public class Obj
        {
            public string p { get; set; }
            public string u { get; set; }
            public List<SD> sD { get; set; }
        }

        public class ObjAssignStudy
        {

            private List<Obj> _ListObj= new List<Obj>();
            internal List<Obj> ListObj
            {
                get { return _ListObj; }
                set { _ListObj = value; }
            }
            List<SD> tempList = new List<SD>(); 
            public ObjAssignStudy()
            {
                tempList.Add(new SD() { s = "EmptyList1" });
                ListObj.Add(new Obj() { p = "a", sD = tempList, u = "B" });
                tempList.Add(new SD() { s = "EmptyList2" });
                ListObj.Add(new Obj() { p = "b", sD =tempList, u = "C" });
                tempList.Add(new SD() { s = "EmptyList3" });
                ListObj.Add(new Obj() { p = "c", sD =tempList, u = "D" });               
            }
            public List<SD> this[string index]
            {
                get
                {
                    foreach (Obj item in ListObj)
                    {
                        if (item.p == index)
                            return item.sD;
                    }
                    return new List<SD>();
                }              
            }
        }
    }
  

EmptyList1   EmptyList2

 List<SD> sampleData=newInstance["a"];// Gives the output
  

EmptyList1   EmptyList2   EmptyList3