2016-11-04 40 views
0

由於某種原因,排序方式並不適用於我的linq查詢。任何人都可以請幫我找到我在這裏做錯了什麼。這裏是我的代碼 -排序方式在MVC Linq查詢中不起作用

var q = (from a in db.Alerts 
       join ap in db.AlertParks on a.AlertId equals ap.AlertId 
       join p in db.Parks on ap.parkId equals p.parkId 
       where a.EndDate > DateTime.Now 
       orderby p.Name ascending 
       select new { a.Title, a.Description, p.Latitude, p.Longitude, a.AlertId, p.Name, a.alertType.IconUrl }) 
       .OrderBy(p=>p.Name) 
       .Concat(from a in db.Alerts 
         join l in db.Locations on a.AlertId equals l.AlertId 
         where a.EndDate > DateTime.Now 
         select new { a.Title, a.Description, l.Latitude, l.Longitude, a.AlertId, l.Name, a.alertType.IconUrl }) 
         .OrderBy(l=>l.Name); 

它返回 -

 


     [ 
    { 
    "Title": "Road closed due to bush fire in Albany", 
    "Description": "what should i write here. I need to fill this at least 100 characters.", 
    "Latitude": -31.9699993134, 
    "Longitude": 116.0599975586, 
    "AlertId": "349dcec1-3d06-49b6-9fcf-0430712f59ef", 
    "Name": "Bibbulmun Track Darling Range", 
    "IconUrl": "~/media/movierental.png" 
    }, 
    { 
    "Title": "This is a test alert to see how it works.", 
    "Description": "some description text", 
    "Latitude": -33.5099983215, 
    "Longitude": 123.3600006104, 
    "AlertId": "b4b3018e-6f70-4dec-a62c-ea402bb0075e", 
    "Name": "Cape Arid", 
    "IconUrl": "~/media/movierental.png" 
    }, 
    { 
    "Title": "This is a test alert to see how it works.", 
    "Description": "fsdf sfsdfsdfsd", 
    "Latitude": -32.02, 
    "Longitude": 115.91, 
    "AlertId": "b4b3018e-6f70-4dec-a62c-ea402bb0075e", 
    "Name": "Canning River", 
    "IconUrl": "~/media/movierental.png" 
    }, 
    { 
    "Title": "fdsfs ", 
    "Description": "fds ", 
    "Latitude": -30.04, 
    "Longitude": 115.58, 
    "AlertId": "cc7c18a5-30f0-4dad-b357-29bd29e828fc", 
    "Name": "Alexander Morrison", 
    "IconUrl": "~/media/sauna.png" 
    }, 
    { 
    "Title": "fdsfs ", 
    "Description": "fds ", 
    "Latitude": -31.94, 
    "Longitude": 116.13, 
    "AlertId": "cc7c18a5-30f0-4dad-b357-29bd29e828fc", 
    "Name": "Beelu", 
    "IconUrl": "~/media/sauna.png" 
    }, 
    { 
    "Title": "fdsfs ", 
    "Description": "fds ", 
    "Latitude": -33.5099983215, 
    "Longitude": 123.3600006104, 
    "AlertId": "cc7c18a5-30f0-4dad-b357-29bd29e828fc", 
    "Name": "Cape Arid", 
    "IconUrl": "~/media/sauna.png" 
    }] 

+0

沒有關係的,但是'排序依據p.Name遞增'和'.OrderBy(p => p.Name)'是不必要的 –

回答

0

我得到它的工作,這裏是修改後的代碼 -

var q = (from a in db.Alerts 
       join ap in db.AlertParks on a.AlertId equals ap.AlertId 
       join p in db.Parks on ap.parkId equals p.parkId 
       // orderby p.Name 
       where a.EndDate<=DateTime.Now 
       select new { a.Title, a.Description, p.Latitude, p.Longitude, a.AlertId, p.Name, a.alertType.IconUrl }) 
       .Concat(from a in db.Alerts 
         join l in db.Locations on a.AlertId equals l.AlertId 
         where a.EndDate <= DateTime.Now 
         select new { a.Title, a.Description, l.Latitude, l.Longitude, a.AlertId, l.Name, a.alertType.IconUrl }) 
         .OrderBy(a=>a.Name);