如何使用虚拟属性返回模型

时间:2019-11-07 15:39:42

标签: c# entity-framework-6

我有一个这样的模型,

[Table("ClientAccessories")]
public class ClientAccessory
{
    public ClientAccessory()
    {
        LastModifiedDateTime = DateTime.UtcNow;
    }

    public string AccessoryId { get; set; }

    public Guid ClientReference { get; set; }

    public DateTime LastModifiedDateTime { get; set; }

    public bool IsActive { get; set; }

    public virtual Accessory Accessory { get; set; }
}

我在存储库方法中有此代码

    public IEnumerable<ClientAccessory> GetClientAccessories(Guid ClientReference)
    {
        var _context = new DBContext();

      var results = from a in _context.Accessories
                         join ca in _context.ClientAccessories
                               on new { AccessoryId = a.Id, ClientReference = new Guid(ClientReference) }
                           equals new { ca.AccessoryId, ca.ClientReference } into ca_join
                         from ca in ca_join.DefaultIfEmpty()
                         where
                           ca.IsActive == true ||
                           ca.IsActive == null
                           select new {};
    }

现在的问题是,即使它是一个虚拟属性,我也不知道如何一起返回ClientAccessory和Accessory对象。

也可以在一个存储库中调用2个实体,还是应该返回IQueryable并在域服务类中进行操作?谢谢。

我不想放平这样的值,

 select new {
 Id = a.Id,
 ClientReference = ca.ClientReference
 and so on...

};

1 个答案:

答案 0 :(得分:0)

如果从ClientAccessory查询并包括附件,您应该得到什么。像这样:

public IEnumerable<ClientAccessory> GetClientAccessories(Guid ClientReference)
{
    var _context = new DBContext();

    var results = from ca in _context.ClientAccessory.Include("Accessory")
        where ca.IsActive == true || ca.IsActive == null
        select ca;
    return results;
}