2010-03-31 35 views
14

Dưới đây là một trong tôi đang làm việc trên:LINQ trật tự bằng cách tổng hợp trong chọn {}

var fStep = 
      from insp in sq.Inspections 
      where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime 
       && insp.Model == "EP" && insp.TestResults != "P" 
      group insp by new { insp.TestResults, insp.FailStep } into grp 

      select new 
      { 
       FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0), 
       CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0), 
       grp.Key.TestResults, 
       grp.Key.FailStep, 
       PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100) 

      } ; 

Tôi muốn orderby một hoặc nhiều trường trong chiếu chọn.

Trả lời

16

Sự thay đổi đơn giản nhất có lẽ là sử dụng một sự tiếp nối truy vấn:

var fStep = 
     from insp in sq.Inspections 
     where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime 
      && insp.Model == "EP" && insp.TestResults != "P" 
     group insp by new { insp.TestResults, insp.FailStep } into grp 
     select new 
     { 
      FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0), 
      CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0), 
      grp.Key.TestResults, 
      grp.Key.FailStep, 
      PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100) 

     } into selection 
     orderby selection.FailedCount, selection.CancelCount 
     select selection; 

Đó là chủ yếu là tương đương với việc sử dụng "phép", phải trung thực - sự khác biệt thực sự là để điều đó giới thiệu một mới phạm vi biến, trong khi tiếp tục truy vấn có hiệu quả bắt đầu phạm vi biến phạm vi mới - bạn không thể tham khảo grp trong bit sau into selection chẳng hạn.

Nó đáng chú ý rằng đây là chính xác giống như cách sử dụng hai báo cáo:

var unordered = 
     from insp in sq.Inspections 
     where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime 
      && insp.Model == "EP" && insp.TestResults != "P" 
     group insp by new { insp.TestResults, insp.FailStep } into grp 
     select new 
     { 
      FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0), 
      CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0), 
      grp.Key.TestResults, 
      grp.Key.FailStep, 
      PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100) 

     }; 

var fStep = from selection in unordered 
      orderby selection.FailedCount, selection.CancelCount 
      select selection; 
+0

Hoạt động tốt –

+0

điều này rất tốt. công việc tốt, jon –

4

bọc toàn bộ truy vấn trong ngoặc đơn và

.OrderBy(x => x.FailedCount).ThenBy(x => x.CancelCount); 
+1

Thứ hai 'OrderBy' nên được 'ThenBy'. –

+0

vui lòng hiển thị ví dụ –

+0

Rất đơn giản. Cảm ơn David. – mack

1

Bạn có thể di chuyển các giá trị chọn một phân let và sau đó xây dựng một trật tự do sau đó.

var fStep = 
    from insp in sq.Inspections 
    where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime 
     && insp.Model == "EP" && insp.TestResults != "P" 
    group insp by new { insp.TestResults, insp.FailStep } into grp 
    let newInsp = new 
    { 
     FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0), 
     CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0), 
     grp.Key.TestResults, 
     grp.Key.FailStep, 
     PercentFailed = Convert.ToDecimal(1.0 * grp.Count()/tcount * 100) 

    } 
    orderby newInsp.FailedCount, newInsp.CancelCount 
    // or this ... 
    //orderby newInsp.FailedCount 
    //orderby newInsp.CancelCount 
    select newInsp; 
; 
+0

Đã xảy ra lỗi khi thực hiện: Không thể sắp xếp theo loại '<> f__AnonymousType6'2 [System.Int32, System.Int32]'. –

+0

thả 'mới ... 'và thay thế bằng' orderby newInsp.FailedCount, newInsp.CancelCount' –

+0

Cảm ơn Thomas ... Không bao giờ cố gắng chạy nó (chỉ có một mô hình dữ liệu giả để đảm bảo ít nhất là biên dịch.) –

Các vấn đề liên quan