4

更新:我有一个 sql 查询工作,但仍然无法将其转换为 linq2sql。请看下面的代码..

我有一个 linq 查询,它正在提取大量数据,但有时由于它在 sql 服务器上的负载而超时。我将致力于获取更有效的数据(添加索引等),但我也听说使用 group by 会比使用子查询更有效。group by 会更有效吗?如果是这样,我的查询在下面的 group by 中会是什么样子?我还不熟悉group by。请不要使用 lambda

编辑:新查询仍然很慢并且可能超时:

  var query = (from s in db.ZipCodeServiceAvailabilities
                         join a in db.pdx_apart_views on s.ZipCode equals a.Zip_Code.Substring(0, 5)  into a_join
                         from a in a_join.DefaultIfEmpty()
                         join b in db.ZipCodeBoundaries on s.ZipCode equals b.ZipCode  into b_join
                         from b in b_join.DefaultIfEmpty()
                         where
                           (s.IsServiced == 1 &&
                            b.Ordering % 10 == 0 &&
                           s.State == "AL")
                         group new { s, b, a } by new
                         {
                             s.ZipCode,
                             s.IsServiced,
                             b.Longitude,
                             b.Latitude,
                             b.Ordering
                         } into g
                         orderby
                           g.Key.ZipCode,
                           g.Key.Ordering
                         select new
                         {
                             g.Key.ZipCode,
                             apartCount = g.Count(p => p.a.Apartment_complex != null),
                             Longitude = g.Key.Longitude,
                             Latitude = g.Key.Latitude
                         }).ToArray();

编辑:在 linq2sql 中查询我想要的 sql 工作(非常快):

select s.ZipCode, count(distinct ident) ApartCount, b.Longitude, b.Latitude from ZipCodeServiceAvailability s
                    left join pdx_apart_view
                          on s.ZipCode = left([Zip Code], 5)
                    left join ZipCodeBoundaries b
                          on s.ZipCode = b.ZipCode


  Where  IsServiced = 1 and and Ordering % 10 = 0 and State = 'AL'

  Group By s.ZipCode, IsServiced, b.Longitude, b.Latitude, b.Ordering
  Order by s.ZipCode, b.Ordering

非常慢的原始查询:

var zips = (from s in db.ZipCodeServiceAvailabilities
    join b in db.ZipCodeBoundaries on s.ZipCode equals b.ZipCode
    where (s.IsServiced == service 
        && b.Ordering % 10 == 0 
        && s.State.Contains(state))
    orderby b.ZipCode
    select new
    {
        zipCode = b.ZipCode.Trim(),
        latitude = b.Latitude,
        longitude = b.Longitude,
        apartCount = (from a in db.pdx_apart_views
                      where a.Zip_Code.Remove(5) == b.ZipCode
                      select a.Apartment_complex).Count()

    }).ToArray();
4

2 回答 2

2

您的意思是您一般不熟悉分组,还是仅在 linq 上下文中不熟悉?

当我没有实体定义时,我发现编写纯 SQL 比 linq 更容易,以下是我认为您在 SQL 中寻找的内容 - 转换回 linq 应该相当简单。

select
  b.ZipCode zipCode,
  b.Latitude latitude,
  b.Longitude longitude,
  count(a.Apartment_complex) apartCount
from 
  ZipCodeServiceAvailabilities s
join ZipCodeBoundaries b 
  on s.ZipCode = b.ZipCode
left join pdx_apart_views a
  on substring(a.Zip_Code, 1, 5) = b.ZipCode
group by
  ZipCode,
  Latitude,
  Longitude
于 2013-02-14T02:01:36.823 回答
1

没有办法在不破坏索引的情况下复制 linq2sql 中的 left[(zip code), 5] sql 方法。答案是直接使用 ado.net,这样我就可以获得所有 sql 功能。

于 2013-02-16T19:09:57.227 回答