LINQ-如何针对不同列选择不同的记录

时间:2018-05-02 07:38:13

标签: c# linq asp.net-web-api

我创建了一个Web API,我试图抓取所有给出日期时间范围的记录。

public HttpResponseMessage GetMeterPing(DateTime start, DateTime end)
{
    try
    {
        var startDateTime = start;
        var endDateTime = end;

        var result = medEntitites.tj_xhqd.Where(m => m.sjsj <= startDateTime && m.sjsj >= endDateTime)
                                         .OrderByDescending(o => o.sjsj)
                                         .Select(s => new { s.zdjh, s.sjsj, s.xhqd })
                                         .Distinct().ToList();
        return Request.CreateResponse(HttpStatusCode.OK, new { data = result});
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }
}

WEB-API网址: http://localhost:14909/api/meters/GetMeterPing/2018-04-27T09:00:00/2018-04-26T21:00:00

响应:

{
"data": [
    {
        "zdjh": "002999000835",
        "sjsj": "2018-04-26T23:59:59",
        "xhqd": "22"
    },
    {
        "zdjh": "002999001089",
        "sjsj": "2018-04-27T00:00:00",
        "xhqd": "19"
    },
    {
        "zdjh": "002998000940",
        "sjsj": "2018-04-27T00:00:01",
        "xhqd": "26"
    },
    {
        "zdjh": "002998000099",
        "sjsj": "2018-04-27T00:00:01",
        "xhqd": "25"
    },
    {
        "zdjh": "002999001180",
        "sjsj": "2018-04-27T00:00:01",
        "xhqd": "17"
    },
    {
        "zdjh": "002998001441",
        "sjsj": "2018-04-27T00:00:01",
        "xhqd": "31"
    },
    {
        "zdjh": "002998002835",
        "sjsj": "2018-04-27T00:00:02",
        "xhqd": "31"
    },
    {
        "zdjh": "002998000549",
        "sjsj": "2018-04-27T00:00:02",
        "xhqd": "29"
    },
    {
        "zdjh": "002998001955",
        "sjsj": "2018-04-27T00:00:02",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000936",
        "sjsj": "2018-04-27T00:00:02",
        "xhqd": "26"
    },
    {
        "zdjh": "002999001113",
        "sjsj": "2018-04-27T00:00:03",
        "xhqd": "24"
    },
    {
        "zdjh": "002999000384",
        "sjsj": "2018-04-27T00:00:03",
        "xhqd": "19"
    },
    {
        "zdjh": "002999000333",
        "sjsj": "2018-04-27T00:00:04",
        "xhqd": "19"
    },
    {
        "zdjh": "002998000718",
        "sjsj": "2018-04-27T00:00:05",
        "xhqd": "22"
    },
    {
        "zdjh": "002999000302",
        "sjsj": "2018-04-27T00:00:05",
        "xhqd": "28"
    },
    {
        "zdjh": "002999000853",
        "sjsj": "2018-04-27T00:00:05",
        "xhqd": "29"
    },
    {
        "zdjh": "002999001183",
        "sjsj": "2018-04-27T00:00:07",
        "xhqd": "23"
    },
    {
        "zdjh": "002998000490",
        "sjsj": "2018-04-27T00:00:07",
        "xhqd": "23"
    },
    {
        "zdjh": "002999000297",
        "sjsj": "2018-04-27T00:00:08",
        "xhqd": "29"
    },
    {
        "zdjh": "002998000579",
        "sjsj": "2018-04-27T00:00:08",
        "xhqd": "31"
    },
    {
        "zdjh": "002999001150",
        "sjsj": "2018-04-27T00:00:09",
        "xhqd": "16"
    },
    {
        "zdjh": "002999000330",
        "sjsj": "2018-04-27T00:00:09",
        "xhqd": "28"
    },
    {
        "zdjh": "002998000103",
        "sjsj": "2018-04-27T00:00:09",
        "xhqd": "13"
    },
    {
        "zdjh": "002998002562",
        "sjsj": "2018-04-27T00:00:09",
        "xhqd": "28"
    },
    {
        "zdjh": "002998003062",
        "sjsj": "2018-04-27T00:00:11",
        "xhqd": "25"
    },
    {
        "zdjh": "002998002178",
        "sjsj": "2018-04-27T00:00:13",
        "xhqd": "19"
    },
    {
        "zdjh": "002999000719",
        "sjsj": "2018-04-27T00:00:13",
        "xhqd": "21"
    },
    {
        "zdjh": "002998002200",
        "sjsj": "2018-04-27T00:00:14",
        "xhqd": "27"
    },
    {
        "zdjh": "002998000126",
        "sjsj": "2018-04-27T00:00:14",
        "xhqd": "23"
    },
    {
        "zdjh": "002999000341",
        "sjsj": "2018-04-27T00:00:15",
        "xhqd": "23"
    },
    {
        "zdjh": "002999001057",
        "sjsj": "2018-04-27T00:00:16",
        "xhqd": "26"
    },
    {
        "zdjh": "002999001017",
        "sjsj": "2018-04-27T00:00:16",
        "xhqd": "25"
    },
    {
        "zdjh": "002998000721",
        "sjsj": "2018-04-27T00:00:16",
        "xhqd": "26"
    },
    {
        "zdjh": "002998000189",
        "sjsj": "2018-04-27T00:00:21",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000417",
        "sjsj": "2018-04-27T00:00:22",
        "xhqd": "25"
    },
    {
        "zdjh": "002999000026",
        "sjsj": "2018-04-27T00:00:24",
        "xhqd": "24"
    },
    {
        "zdjh": "002998004535",
        "sjsj": "2018-04-27T00:00:25",
        "xhqd": "17"
    },
    {
        "zdjh": "002999000924",
        "sjsj": "2018-04-27T00:00:26",
        "xhqd": "21"
    },
    {
        "zdjh": "002998001434",
        "sjsj": "2018-04-27T00:00:26",
        "xhqd": "28"
    },
    {
        "zdjh": "002999000923",
        "sjsj": "2018-04-27T00:00:28",
        "xhqd": "27"
    },
    {
        "zdjh": "002998001753",
        "sjsj": "2018-04-27T00:00:30",
        "xhqd": "23"
    },
    {
        "zdjh": "002999000161",
        "sjsj": "2018-04-27T00:00:31",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000507",
        "sjsj": "2018-04-27T00:00:32",
        "xhqd": "20"
    },
    {
        "zdjh": "002999000410",
        "sjsj": "2018-04-27T00:00:32",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000714",
        "sjsj": "2018-04-27T00:00:33",
        "xhqd": "16"
    },
    {
        "zdjh": "002998000805",
        "sjsj": "2018-04-27T00:00:34",
        "xhqd": "22"
    },
    {
        "zdjh": "002998001846",
        "sjsj": "2018-04-27T00:00:35",
        "xhqd": "29"
    },
    {
        "zdjh": "002998003251",
        "sjsj": "2018-04-27T00:00:36",
        "xhqd": "26"
    },
    {
        "zdjh": "002999000050",
        "sjsj": "2018-04-27T00:00:37",
        "xhqd": "25"
    },
    {
        "zdjh": "002999000071",
        "sjsj": "2018-04-27T00:00:38",
        "xhqd": "22"
    },
    {
        "zdjh": "002999000728",
        "sjsj": "2018-04-27T00:00:39",
        "xhqd": "10"
    },
    {
        "zdjh": "002999001134",
        "sjsj": "2018-04-27T00:00:41",
        "xhqd": "30"
    },
    {
        "zdjh": "002999000258",
        "sjsj": "2018-04-27T00:00:42",
        "xhqd": "29"
    },
    {
        "zdjh": "002999001123",
        "sjsj": "2018-04-27T00:00:45",
        "xhqd": "30"
    },
    {
        "zdjh": "002999001156",
        "sjsj": "2018-04-27T00:00:46",
        "xhqd": "31"
    },
    {
        "zdjh": "002998000136",
        "sjsj": "2018-04-27T00:00:47",
        "xhqd": "19"
    },
    {
        "zdjh": "002999000260",
        "sjsj": "2018-04-27T00:00:47",
        "xhqd": "19"
    },
    {
        "zdjh": "002999001097",
        "sjsj": "2018-04-27T00:00:48",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000197",
        "sjsj": "2018-04-27T00:00:48",
        "xhqd": "24"
    },
    {
        "zdjh": "002998003204",
        "sjsj": "2018-04-27T00:00:50",
        "xhqd": "19"
    },
    {
        "zdjh": "002999000811",
        "sjsj": "2018-04-27T00:00:53",
        "xhqd": "26"
    },
    {
        "zdjh": "002998004243",
        "sjsj": "2018-04-27T00:00:55",
        "xhqd": "20"
    },
    {
        "zdjh": "002999000737",
        "sjsj": "2018-04-27T00:00:55",
        "xhqd": "29"
    },
    {
        "zdjh": "002998002573",
        "sjsj": "2018-04-27T00:01:02",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000112",
        "sjsj": "2018-04-27T00:01:05",
        "xhqd": "27"
    },
    {
        "zdjh": "002998001569",
        "sjsj": "2018-04-27T00:01:05",
        "xhqd": "25"
    },
    {
        "zdjh": "002998003111",
        "sjsj": "2018-04-27T00:01:08",
        "xhqd": "23"
    },
    {
        "zdjh": "002999000627",
        "sjsj": "2018-04-27T00:01:08",
        "xhqd": "13"
    },
    {
        "zdjh": "002999000296",
        "sjsj": "2018-04-27T00:01:10",
        "xhqd": "24"
    },
    {
        "zdjh": "002999001094",
        "sjsj": "2018-04-27T00:01:12",
        "xhqd": "18"
    },
    {
        "zdjh": "002999000776",
        "sjsj": "2018-04-27T00:01:13",
        "xhqd": "19"
    },
    {
        "zdjh": "002999001040",
        "sjsj": "2018-04-27T00:01:16",
        "xhqd": "19"
    },
    {
        "zdjh": "002998003531",
        "sjsj": "2018-04-27T00:01:17",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000955",
        "sjsj": "2018-04-27T00:01:18",
        "xhqd": "20"
    },
    {
        "zdjh": "002998002658",
        "sjsj": "2018-04-27T00:01:26",
        "xhqd": "29"
    },
    {
        "zdjh": "002999000444",
        "sjsj": "2018-04-27T00:01:30",
        "xhqd": "15"
    },
    {
        "zdjh": "002998001837",
        "sjsj": "2018-04-27T00:01:34",
        "xhqd": "31"
    },
    {
        "zdjh": "002999001144",
        "sjsj": "2018-04-27T00:01:35",
        "xhqd": "23"
    },
    {
        "zdjh": "002999000245",
        "sjsj": "2018-04-27T00:01:42",
        "xhqd": "31"
    },
    {
        "zdjh": "002999001157",
        "sjsj": "2018-04-27T00:01:45",
        "xhqd": "30"
    },
    {
        "zdjh": "002998002127",
        "sjsj": "2018-04-27T00:01:47",
        "xhqd": "17"
    },
    {
        "zdjh": "002999000804",
        "sjsj": "2018-04-27T00:01:48",
        "xhqd": "14"
    },
    {
        "zdjh": "002999001160",
        "sjsj": "2018-04-27T00:01:50",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000241",
        "sjsj": "2018-04-27T00:01:51",
        "xhqd": "27"
    },
    {
        "zdjh": "002999000705",
        "sjsj": "2018-04-27T00:01:51",
        "xhqd": "24"
    },
    {
        "zdjh": "002998003209",
        "sjsj": "2018-04-27T00:01:53",
        "xhqd": "19"
    },
    {
        "zdjh": "002999001104",
        "sjsj": "2018-04-27T00:01:55",
        "xhqd": "25"
    },
    {
        "zdjh": "002993000002",
        "sjsj": "2018-04-27T00:01:58",
        "xhqd": "25"
    },
    {
        "zdjh": "002998003455",
        "sjsj": "2018-04-27T00:01:59",
        "xhqd": "22"
    },
    {
        "zdjh": "002999000205",
        "sjsj": "2018-04-27T00:01:59",
        "xhqd": "25"
    },
    {
        "zdjh": "002999000835",
        "sjsj": "2018-04-27T00:02:01",
        "xhqd": "21"
    },
    {
        "zdjh": "002999001089",
        "sjsj": "2018-04-27T00:02:01",
        "xhqd": "19"
    },
    {
        "zdjh": "002999000936",
        "sjsj": "2018-04-27T00:02:02",
        "xhqd": "25"
    },
    {
        "zdjh": "002998001862",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "31"
    },
    {
        "zdjh": "002998003582",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "14"
    },
    {
        "zdjh": "002998002839",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "27"
    },
    {
        "zdjh": "002998001520",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "31"
    },
    {
        "zdjh": "002999001180",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "17"
    },
    {
        "zdjh": "002998003174",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "31"
    },
    {
        "zdjh": "002998003074",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "27"
    },
    {
        "zdjh": "002998004583",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "31"
    },
    {
        "zdjh": "002998003018",
        "sjsj": "2018-04-27T00:02:03",
        "xhqd": "25"
    },
    {
        "zdjh": "002998003594",
        "sjsj": "2018-04-27T00:02:04",
        "xhqd": "31"
    },
    {
        "zdjh": "002998000738",
        "sjsj": "2018-04-27T00:02:04",
        "xhqd": "28"
    },
    {
        "zdjh": "002998001956",
        "sjsj": "2018-04-27T00:02:04",
        "xhqd": "31"
    },
    {
        "zdjh": "002999000384",
        "sjsj": "2018-04-27T00:02:05",
        "xhqd": "19"
    },
    {
        "zdjh": "002998003577",
        "sjsj": "2018-04-27T00:02:05",
        "xhqd": "14"
    },
    {
        "zdjh": "002998003085",
        "sjsj": "2018-04-27T00:02:05",
        "xhqd": "31"
    },
    {
        "zdjh": "002998002079",
        "sjsj": "2018-04-27T00:02:05",
        "xhqd": "22"
    },
    {
        "zdjh": "002999001113",
        "sjsj": "2018-04-27T00:02:05",
        "xhqd": "24"
    },
    {
        "zdjh": "002999000333",
        "sjsj": "2018-04-27T00:02:05",
        "xhqd": "18"
    },
    {
        "zdjh": "002998002644",
        "sjsj": "2018-04-27T00:02:05",
        "xhqd": "31"
    },
    {
        "zdjh": "002998003087",
        "sjsj": "2018-04-27T00:02:05",
        "xhqd": "27"
    },
    {
        "zdjh": "002998003173",
        "sjsj": "2018-04-27T00:02:06",
        "xhqd": "20"
    },
    {
        "zdjh": "002999000302",
        "sjsj": "2018-04-27T00:02:06",
        "xhqd": "26"
    },
    {
        "zdjh": "002998003263",
        "sjsj": "2018-04-27T00:02:06",
        "xhqd": "14"
    },
    {
        "zdjh": "002998001651",
        "sjsj": "2018-04-27T00:02:06",
        "xhqd": "26"
    },
    {
        "zdjh": "002999000853",
        "sjsj": "2018-04-27T00:02:06",
        "xhqd": "29"
    },
    {
        "zdjh": "002998001783",
        "sjsj": "2018-04-27T00:02:06",
        "xhqd": "25"
    },
    {
        "zdjh": "002998002694",
        "sjsj": "2018-04-27T00:02:06",
        "xhqd": "23"
    },
    {
        "zdjh": "002998001409",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "22"
    },
    {
        "zdjh": "002998003581",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "10"
    },
    {
        "zdjh": "002998002685",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "28"
    },
    {
        "zdjh": "002998004510",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "25"
    },
    {
        "zdjh": "002998001644",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "22"
    },
    {
        "zdjh": "002998002578",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "25"
    },
    {
        "zdjh": "002998000310",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "23"
    },
    {
        "zdjh": "002998003203",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "19"
    },
    {
        "zdjh": "002998002163",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "16"
    },
    {
        "zdjh": "002998000531",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "22"
    },
    {
        "zdjh": "002998001955",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "31"
    },
    {
        "zdjh": "002998000246",
        "sjsj": "2018-04-27T00:02:07",
        "xhqd": "22"
    },
    {
        "zdjh": "002998003015",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "27"
    },
    {
        "zdjh": "002999001183",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "24"
    },
    {
        "zdjh": "002998002637",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "13"
    },
    {
        "zdjh": "002998003086",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "29"
    },
    {
        "zdjh": "002998001980",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "23"
    },
    {
        "zdjh": "002998003163",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "19"
    },
    {
        "zdjh": "002998001416",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "14"
    },
    {
        "zdjh": "002998003036",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "22"
    },
    {
        "zdjh": "002998003088",
        "sjsj": "2018-04-27T00:02:08",
        "xhqd": "21"
    },
    {
        "zdjh": "002999000297",
        "sjsj": "2018-04-27T00:02:09",
        "xhqd": "29"
    },
    {
        "zdjh": "002998000940",
        "sjsj": "2018-04-27T00:02:09",
        "xhqd": "26"
    },
    {
        "zdjh": "002998001507",
        "sjsj": "2018-04-27T00:02:09",
        "xhqd": "22"
    },
    {
        "zdjh": "002998001968",
        "sjsj": "2018-04-27T00:02:09",
        "xhqd": "25"
    },
    {
        "zdjh": "002998002568",
        "sjsj": "2018-04-27T00:02:09",
        "xhqd": "18"
    },
    {
        "zdjh": "002998001949",
        "sjsj": "2018-04-27T00:02:10",
        "xhqd": "22"
    },
    {
        "zdjh": "002999000330",
        "sjsj": "2018-04-27T00:02:10",
        "xhqd": "28"
    },
    {
        "zdjh": "002998003181",
        "sjsj": "2018-04-27T00:02:10",
        "xhqd": "27"
    },
    {
        "zdjh": "002998003253",
        "sjsj": "2018-04-27T00:02:10",
        "xhqd": "26"
    },
    {
        "zdjh": "002999001150",
        "sjsj": "2018-04-27T00:02:10",
        "xhqd": "17"
    },
    {
        "zdjh": "002998000746",
        "sjsj": "2018-04-27T00:02:10",
        "xhqd": "25"
    },
    {
        "zdjh": "002998002686",
        "sjsj": "2018-04-27T00:02:10",
        "xhqd": "24"
    },
    {
        "zdjh": "002998003035",
        "sjsj": "2018-04-27T00:02:10",
        "xhqd": "20"
    },
    {
        "zdjh": "002998000733",
        "sjsj": "2018-04-27T00:02:11",
        "xhqd": "20"
    },
    {
        "zdjh": "002998001829",
        "sjsj": "2018-04-27T00:02:11",
        "xhqd": "29"
    },
    {
        "zdjh": "002998003206",
        "sjsj": "2018-04-27T00:02:12",
        "xhqd": "20"
    },      

 ]
}

注意: zdjh是米号,sjsj是米的ping日期时间,xhqd是ping值。

现在在上面的响应中,有多个相同zdjh的值。我想设置我的LINQ以便在单个zdjh =&gt;米号上返回,其最后sjsj =&gt; ping日期时间值和最后xhqd =&gt; ping值

如何设置LINQ?任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

这是你想要实现的目标吗?

medEntitites.tj_xhqd.OrderByDescending(x => x.zdjh).ThenByDescending(x => x.sjsj).ThenByDescending(x => x.xhqd).DistinctBy(x => x.zdjh).ToList();

区别于MoreLinq GitHub