将ToList()的结果从集线器发送到客户端

时间:2018-06-12 06:36:49

标签: entity-framework linq signalr

这是我在Entity Framework Code-Firts的课程(我把它缩小了):

public class dslam
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }

    [ForeignKey("center")]
    public int centerId { get; set; }
    public center center { get; set; }

    [MaxLength(50)]
    [DisplayName("Title")]
    public string title { get; set; }

    [MaxLength(20)]
    [DisplayName("Type")]
    public string type { get; set; }

    [MaxLength(5)]
    [DisplayName("Port")]
    public string port { get; set; }

    [MaxLength(20)]
    [DisplayName("Host")]
    public string host { get; set; }
}

这是客户端代码:

detailHub.client.setDSLAMInfo = function (liDslams) {
    alert('test');
}

如果我像这样使用 dslam 类,alert('test');会在我的客户端触发:

List<dslam> liDSLAMS = new List<dslam>();
liDSLAMS.Add(new dslam { title = "1", Type= "2", Port= "3"  });

var foo = JsonConvert.SerializeObject(liDSLAMS);
Clients.Client(hubConnectionId).setDSLAMInfo(foo);

但是我无法使用ToList(),似乎ToList()会破坏某些内容而alert('test');不会触发:

List<dslam> liDSLAMS = new List<dslam>();
liDSLAMS = ed.dslams.Where(x => x.centerId == 1).ToList();

//this line will not work properly I guess ToList(); broke something
//var foo = JsonConvert.SerializeObject(liDSLAMS);

Clients.Client(hubConnectionId).setDSLAMInfo(foo);

修改

liDSLAMS不为空: enter image description here

1 个答案:

答案 0 :(得分:0)

好的,问题在于Navigation Properties

似乎无法将具有导航属性的对象发送到客户端。它们也不能转换为 JSON 。只需选择除导航属性之外的所需属性。

var liDSLAM = ed.dslams.Where(x => x.centerId == 1).Select(x => new { x.id, x.title }).ToList();
Clients.Client(hubConnectionId).setDSLAMInfo(liDSLAM);