如何将JSON对象数组转换为JSON字符串数组?

时间:2017-01-04 12:06:58

标签: json entity-framework linq entity-framework-6 linq-to-entities

我的代码如下所示,我得到以下输出,输出很好但我只想在逗号分隔的位置列表中格式化输出。怎么做?

 var eventDetails = (from e in db.tb_Event                            
                      select new
                      {
                         e.EventID,
                         e.tb_Customer.CustomerName,
                         e.StartDate,
                         e.EndDate,
                         loc = (from l in db.tb_EventLocation where 
                                   l.EventID == e.EventID 
                                   select new { l.tb_Location.LocationName })
                                  .Distinct(),
                         e.Objective
                     });

输出结果为:

[
    {
        "EventID": 1,
        "CustomerName": "qwe",
        "StartDate": null,
        "EndDate": null,
        "loc": [
            {
                "LocationName": "asd"
            },
            {
                "LocationName": "zxc"
            }
        ],
        "Objective": "Floor Walkthrough"
    },
    {
        "EventID": 2,
        "CustomerName": "rtg",
        "StartDate": null,
        "EndDate": null,
        "loc": [
            {
                "LocationName": "asd"
            }
        ],
        "Objective": "RFP"
    },
    {
        "EventID": 3,
        "CustomerName": "zxc",
        "StartDate": null,
        "EndDate": null,
        "loc": [],
        "Objective": "RFI"
    }
]

我希望loc来

[{"LocationName":"asd","zxc"}] 

即。逗号分隔的位置列表。怎么做?

3 个答案:

答案 0 :(得分:0)

首先尝试对位置进行分组,然后尝试所有组ID:

 loc = (from l in db.tb_EventLocation 
        where l.EventID == e.EventID 
        group  l.tb_Location.LocationName by l.tb_Location.LocationName
        select new { g.Key})

答案 1 :(得分:0)

我试图通过对代码进行一些更改来实现此目的:

var eventDetails = (from e in db.tb_Event
                                select new
                                {
                                    e.EventID,
                                    e.tb_Customer.CustomerName,
                                    e.StartDate,
                                    e.EndDate,
                                    loc = (from l in db.tb_EventLocation where 
                                                l.EventID == e.EventID 
                                                select new { l.tb_Location.LocationName })
                                                .Distinct().ToArray().CombineWith(","),
                                    e.Objective
                                });

CombineWith是一种扩展方法,只是为了保持代码的可读性:

private static string CombineWith (this string[] locationNames,string delimiter )
{
  return string.Join(delimiter,locationNames);   
}

仅供注意:

[{"LocationName":"asd","zxc"}]不是一个有效的杰森。您可能需要以下内容:[{"LocationName":"asd,zxc"}]

答案 2 :(得分:0)

我使用一个简单的方法从一组实体创建一个JArray。

注意:数据是存储实体的原始json的列

看看:

 private string RawJsonFrom(IEnumerable<TEntity> entities)
 {
        JArray array = new JArray();
        foreach (vare in entities)            
            array.Add(JObject.Parse(e.Data));            

        return array.ToString();
 }