选择Lambda Expression并检查Null Value

时间:2012-11-21 19:28:30

标签: c# .net entity-framework

public List<CompanyRatingInfo> GetTopCompanies()
        {
            return objectContext.Companies.OrderBy(c => c.Reviews.Sum(r => r.Rating))
                .Select(co => new CompanyRatingInfo()
                {
                    CompanyID = co.CompanyID,
                    CompanyName = co.Name,
                    CreateDate = co.CreateDate,
                    Logo = co.Logo,
                    Rating = co.Rating ?? 0,
                    ReviewsCount = (co.Reviews != null ? co.Reviews.Count : 0),
                    WebSite = co.Website
                }).ToList();
        }



错误: 无法比较'System.Data.Objects.DataClasses.EntityCollection`1'类型的元素。仅支持原始类型(如Int32,String和Guid)和实体类型。


如何通过检查co.Reviews获得ReviewsCount!= null?

`Company` has many `Reviews` which has `Rating`

1 个答案:

答案 0 :(得分:2)

应该是co.Reviews.Count()。伯爵是一种方法。