简体   繁体   English

选择Lambda Expression并检查Null Value

[英]Select by Lambda Expression And Check Null Value

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();
        }



ERROR: Cannot compare elements of type 'System.Data.Objects.DataClasses.EntityCollection`1'. 错误: 无法比较'System.Data.Objects.DataClasses.EntityCollection`1'类型的元素。 Only primitive types (such as Int32, String, and Guid) and entity types are supported. 仅支持基本类型(例如Int32,String和Guid)和实体类型。


How can i obtain ReviewsCount with checking co.Reviews != null ? 我怎样才能获得ReviewsCount与检查co.Reviews!= NULL?

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

It should be co.Reviews.Count() . 它应该是co.Reviews.Count() Count is a method. 伯爵是一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM