Order by在MVC Linq查询中不起作用

时间:2016-11-04 04:40:00

标签: linq

由于某种原因,order by不能正常运行我的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"
    }]

1 个答案:

答案 0 :(得分: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);