实体框架到json - 分组数据

时间:2015-05-24 23:34:11

标签: c# json entity-framework serialization

我有一个名为客户端的类,如下所示:

public class Client
{

    [Key, ForeignKey("BaseAssignments")]
    public int ClientId { get; set; }
    public string Owner { get; set; }
    public string CompanyName { get; set; }

    public virtual ICollection<BaseAssignment> BaseAssignments { get; set; }
}

一个名为 Base 的类看起来像这样:

public class Base
{
    [Key, ForeignKey("BaseAssignments")]
    public int BaseId { get; set; }
    public string BaseName { get; set; }
    public DateTime BaseStart { get; set; }
    public DateTime BaseEnd { get; set; }

    public virtual ICollection<BaseAssignment> BaseAssignments { get; set; }
}

他们将与另一个名为 BaseAssignment

的班级合并
public class BaseAssignment
{
    [Key]
    public int BaseAssignmentId { get; set; }
    public int BaseId { get; set; }
    public int ClientId { get; set; }

    public virtual Base Base { get; set; }
    public virtual Client Client { get; set; }
}

这个想法是可以将客户端分配到许多基础,并且一个基础可以包含许多客户端。

继续前进,我试图以这样的方式序列化基本权限,即基础的json表示将所有客户端的集合作为子对象。我试图实现这一目标的Web Api方法是:

db.Configuration.ProxyCreationEnabled = false;
var query = from b in db.Bases
    group b by b.BaseId into nb
    join ba in db.BaseAssignments on nb.FirstOrDefault().BaseId equals ba.BaseId
    join c in db.Clients on ba.ClientId equals c.ClientId
    select new BaseDTO
    {
        BaseName = nb.FirstOrDefault().BaseName,
        BaseStart = nb.FirstOrDefault().BaseStart,
        BaseEnd = nb.FirstOrDefault().BaseEnd,
        Clients = from c1 in db.Clients select new ClientDTO
        {
            ClientId = c1.ClientId,
            CompanyName = c1.CompanyName,
            Owner = c1.Owner
        }
    };
return query;

其中 BaseDTO 如下所示:

public class BaseDTO
{
    public String BaseName { get; set; }
    public DateTime BaseStart { get; set; }
    public DateTime BaseEnd { get; set; }

    public IQueryable<ClientDTO> Clients { get; set; }
}

ClientDTO 如下所示:

public class ClientDTO
{
    public int ClientId { get; set; }
    public string Owner { get; set; }
    public string CompanyName { get; set; }
}

截至目前,我收到一条错误消息,指出ClientDTO属于意外类型。我能做些什么来解决这个问题,或者我选择的方式是完全错误的?提前感谢您对此有任何见解。

修改

我对Web Api控制器方法做了一些更改,所以它看起来像:

db.Configuration.ProxyCreationEnabled = false;
var query = from b in db.Bases
    group b by b.BaseId into nb
    join ba in db.BaseAssignments on nb.FirstOrDefault().BaseId equals ba.BaseId
    join c in db.Clients on ba.ClientId equals c.ClientId
    select new BaseDTO
    {
        BaseName = nb.FirstOrDefault().BaseName,
        BaseStart = nb.FirstOrDefault().BaseStart,
        BaseEnd = nb.FirstOrDefault().BaseEnd,
        Clients = new ClientDTO
        {
            ClientId = c.ClientId,
            CompanyName = c.CompanyName,
            Owner = c.Owner
        }
    };
return query;

这使得Api产生了一个JSON,但它仍然包含每个客户端的单个对象,而不是每个基础。

2 个答案:

答案 0 :(得分:2)

你不应该为此手动分组或加入任何东西,只需使用一个子选择并让LINQ解决繁重的问题。

from b in db.Bases
select new BaseDTO
{
    BaseName = b.BaseName,
    BaseStart = b.BaseStart,
    BaseEnd = b.BaseEnd,
    Clients = 
        from ba in b.BaseAssignments
        from c in ba.Client
        select new ClientDTO
        {
            ClientId = c.ClientId,
            CompanyName = c.CompanyName,
            Owner = c.Owner
        }
}

答案 1 :(得分:0)

基于StriplingWarrior's建议,将两个方面的优点融合到:

from b in db.Bases
select new BaseDTO
{
    BaseName = b.BaseName,
    BaseStart = b.BaseStart,
    BaseEnd = b.BaseEnd,
    Clients =
        from ba in b.BaseAssignments 
        join c in db.Clients on ba.ClientId equals c.ClientId
        select new ClientDTO
        {
            ClientId = c.ClientId,
            CompanyName = c.CompanyName,
            Owner = c.Owner
        }
 };

得到了我想要的JSON - 谢谢。