具有导航属性且没有导航属性的实体框架查询

时间:2017-04-04 12:31:52

标签: c# entity-framework linq query-performance

以下是我的两个模型类:

public partial class Region
{
        public int Id { get; set; }
        public virtual ICollection<SubRegions_Coordinates> SubRegions_Coordinates { get; set; }
}

public partial class SubRegions_Coordinates
{
        public int Id { get; set; }
        public Nullable<int> RegionId { get; set; }
        public Nullable<int> AreaId { get; set; }
        public string Latitude { get; set; }

        public virtual Area Area { get; set; }
        public virtual Region Region { get; set; }
}

查询:

var query = from r in context.Region
            select new 
            {
               Latitude = r.SubRegions_Coordinates.
                          Where(sb =>sb.AreaId== 100).Select(sb=>sb.Latitude).FirstOrDefault(),
            }

var query = from r in context.Region
            select new 
            {
                 Latitude = context.SubRegions_Coordinates.Where(sb => sb.RegionId == r.Id && sb.AreaId == 100).
                            Select(sb => sb.Latitude).FirstOrDefault(),
               }

以上是我的两个查询,它们基本上为特定Areaid = 100的每个区域获取纬度。

但我不明白上述两个查询中的哪一个会更好地表现性能和原因。

我应该使用哪种查询?

0 个答案:

没有答案
相关问题