获取父对象及其子计数而不使用匿名类型

时间:2016-12-03 15:43:55

标签: c# entity-framework linq

所以我有下一个方法(可行)返回claims加上observations的列表。一项索赔可以有零或多次观察。代码工作,但我担心它是乱七八糟的,使用匿名类型,然后将其解析为一个新的声明类型,设置计数。

public async Task<IEnumerable<Claim>> GetClaims(ClaimStatusCode status, int take = 10, int skip = 0)
{
    using (var db = new DataContext())
    {    
        var pendingclaims = await (from claim in db.Claims
                                where claim.OfficeCode == _officeCode
                                where claim.ClaimStatusCode == status
                                select new
                                {
                                    ID = claim.ID,
                                    ClaimStatusCode = claim.ClaimStatusCode,
                                    OpenDate = claim.OpenDate,
                                    LastUpdateDate = claim.LastUpdateDate,
                                    CloseDate = claim.CloseDate,
                                    ProductCode = claim.ProductCode,
                                    IssueCode = claim.IssueCode,
                                    SpecificIssueCode = claim.SpecificIssueCode,
                                    OfficeCode = claim.OfficeCode,
                                    Summary = claim.Summary,
                                    ObservationsCount = claim.Observations.Count
                                }).OrderBy(c => c.OpenDate).Take(take).Skip(skip).ToListAsync();                     

        var list = new List<Claim>();
        foreach (var claim in pendingclaims)
        {
            Claim c = new Claim()
            {
                ID = claim.ID,
                ClaimStatusCode = claim.ClaimStatusCode,
                OpenDate = claim.OpenDate,
                LastUpdateDate = claim.LastUpdateDate,
                CloseDate = claim.CloseDate,
                ProductCode = claim.ProductCode,
                IssueCode = claim.IssueCode,
                SpecificIssueCode = claim.SpecificIssueCode,
                OfficeCode = claim.OfficeCode,
                Summary = claim.Summary,
                ObservationsCount = claim.ObservationsCount
            };
            list.Add(c);
        }

        return list;
    }
}

我想也许我遗漏了一些东西来减少结果SQL查询的混乱,但是不知道是什么。有什么想法吗?

更新

根据要求,这是声明和观察课程,我使用的是一个简单的实体代码First One to Many关系:

权利要求

public class Claim
{
    public Claim()
    {
        Observations = new List<Observation>();
    }

    [Key]
    public Guid ID { get; set; }

    ...

    public virtual ICollection<Observation> Observations { get; set; }

    [NotMapped]
    public int ObservationsCount { get; set; }
}

观察

public class Observation
{
    public Observation()
    { }

    [Key]
    public Guid ID { get; set; }

    ...

    public virtual Guid ClaimID { get; set; }

    [ForeignKey("ClaimID")]
    public virtual Claim Claim { get; set; }
}

1 个答案:

答案 0 :(得分:3)

如果没有一些中间投影(无论是匿名类型还是具体类型,只要它不是实体类型),EF6就没有办法得到你想要的东西。但是,如果您需要所有对象字段和子计数,您可以简化实现,如下所示:

var pendingclaims = await (from claim in db.Claims.AsNoTracking()
                           where claim.OfficeCode == _officeCode
                           where claim.ClaimStatusCode == status
                           orderby claim.OpenDate
                           select new
                           {
                               claim,
                               ObservationsCount = claim.Observations.Count
                           }).Take(take).Skip(skip).ToListAsync();                     

return pendingclaims.Select(item =>
{
    item.claim.ObservationsCount = item.ObservationsCount;
    return item.claim;
}).ToList();