LINQ分组对象

时间:2015-12-03 07:11:09

标签: c# linq grouping

我有两个班级

public class MyObjects{
  public bool Active {get; set;}
  public List<OtherObject> OtherObjects {get; set;}
}

public class OtherObject {
  public int Id {get; set;}
  public bool Enabled {get; set;}
  public string Address {get; set;}
  public string Name {get; set;}
}

我目前的结果是

MyObject { Active = true; }, 
OtherObjects: [OtherObject: { Id: 1, Name: 'First'}, 
OtherObject{Id: 2, Name: 'First'}, 
OtherObject{Id: 3, Name: 'Second'}];

我希望按Name对它们进行分组,因此我仍然拥有Active属性,而OtherObjects内部的OtherObject属于Name { ""Active"": true, ""OtherObjects"": [ { ""ObjectName"": ""Second"", ""ObjectOtherProperties"": [ { ""Id"": 1, ""Enabled"": false }, { ""Id"": 2, ""Enabled"": true } ], ""ObjectName"": ""Second"", ""ObjectOtherProperties"": [ { ""Id"": 1, ""Enabled"": false } ], ] } } 属性。是否可以仅使用LINQ?

修改 最终的结果应该是json,我将用于角度,所以它应该是这样的:

{{1}}

有任何建议如何实现这一目标?也许我必须制作其他课程并以某种方式通过分组来映射它们?

2 个答案:

答案 0 :(得分:1)

我就是这样做的,保持简单:

// 1. Add OtherObjectsDictionary
// 2. Block OtherObjects in the json serialization
public class MyObjects
{

    public bool Active { get; set; }

    [Newtonsoft.Json.JsonIgnore]
    public List<OtherObject> OtherObjects { get; set; }

    public Dictionary<string, List<OtherObject>> OtherObjectsDictionary { get; set; }

}

// 3. Block Name in the json serialization
public class OtherObject
{

    public int Id { get; set; }

    public bool Enabled { get; set; }

    public string Address { get; set; }

    [Newtonsoft.Json.JsonIgnore]
    public string Name { get; set; }

}

// 4. Linq queries to achieve the grouped result
// 5. Serialize to Json
static void Main(string[] args)
{

    var myObjects = new MyObjects() { Active = true, OtherObjects = new List<OtherObject>() };
    myObjects.OtherObjects.Add(new OtherObject { Id = 1, Name = "First" });
    myObjects.OtherObjects.Add(new OtherObject { Id = 2, Name = "First" });
    myObjects.OtherObjects.Add(new OtherObject { Id = 3, Name = "Second" });

    myObjects.OtherObjectsDictionary = new Dictionary<string, List<OtherObject>>();
    var distinctNames = myObjects.OtherObjects.Select(otherObject => otherObject.Name).Distinct();
    foreach(var distinctName in distinctNames)
    {
        var groupedObjectsList = myObjects.OtherObjects.Where(otherObject => otherObject.Name == distinctName).ToList();
        myObjects.OtherObjectsDictionary.Add(distinctName, groupedObjectsList);
    }

    var outputJson = Newtonsoft.Json.JsonConvert.SerializeObject(myObjects);

}   

这是json的结果:

{
  "Active": true,
  "OtherObjectsDictionary": {
    "First": [
      {
        "Id": 1,
        "Enabled": false,
        "Address": null
      },
      {
        "Id": 2,
        "Enabled": false,
        "Address": null
      }
    ],
    "Second": [
      {
        "Id": 3,
        "Enabled": false,
        "Address": null
      }
    ]
  }
}

我希望它有所帮助。

答案 1 :(得分:1)

您也可以使用System.Web.Extensions .dll作为框架4.0项目的添加引用(不是4.0客户端配置文件)。

enter image description here

然后在课堂内添加。 enter image description here

我还应用了一种不同的方法,一种或多或少的数据库,如规范化。

课程列表

        public class MyObjects
        {
            public bool Active { get; set; }
            public List<ObjectName> OtherObjects { get; set; }
        }

        public class ObjectName
        {
            public string Name { get; set; }
            public List<OtherObject> OtherObjectProperties { get; set; }
        }

        public class OtherObject
        {
            public int Id { get; set; }
            public bool Enabled { get; set; }
            [ScriptIgnore]
            public string Address { get; set; }
            [ScriptIgnore]
            public string Name { get; set; }
        }

填充记录..

            List<OtherObject> oList = new List<OtherObject>();
            oList.Add(new OtherObject() { Id = 2, Name = "First" });
            oList.Add(new OtherObject() { Id = 3, Name = "Second" });

            // each name with objects
            List<ObjectName> oNames = new List<ObjectName>();
            oNames.AddRange(oList.Select(p => new ObjectName() { 
                   Name = p.Name
                   , OtherObjectProperties = oList.Where(p1 => p1.Name == p.Name).ToList()
            }).Distinct()
            );

            // parent object with with object names
            MyObjects mo = new MyObjects() { Active = true, OtherObjects = oNames };

最后,javascript序列化..

JavaScriptSerializer jss = new JavaScriptSerializer();
string b = jss.Serialize(mo);

字符串b应该为您提供如下输出..

{
"Active":true
,"OtherObjects":[
{
    "Name":"First"
    ,"OtherObjectProperties":[
    {
        "Id":2
        ,"Enabled":false}
    ]},
    {
    "Name":"Second"
    ,"OtherObjectProperties":[
    {
        "Id":3
        ,"Enabled":false}
    ]
}]
}

请告知您是否对以下任何内容感到困惑.. :)