Linq查询以获取人员当天的访问区域 [英] Linq query to get person visited zones of current day

查看:135
本文介绍了Linq查询以获取人员当天的访问区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity框架作为ADO.NET的对象关系映射(ORM)框架来开发Web api.我需要编写一个linq查询,该查询应返回一个人旅行的最近5个区域.

I am working on web api with Entity framework as object-relational mapping (ORM) framework for ADO.NET. I need to write a linq query which should return most recent 5 zones traveled by a person.

我的带有相应列的表格在所附的图像中进行了描述.我使用azure sql数据库作为后端存储,从上述数据 我需要使用linq查询获取前5个zone_id列表为[4,2,3,2,1] .客户可能会要求获取在 stime 的特定范围内的区域列表.

My table with respective columns is depicted in the attached image. I am using azure sql database as my back-end storage, from the above data i need to get top 5 zone_id list as [4,2,3,2,1] by using linq query. client may request to get zones list with in specific range of stime.

推荐答案

不是100%知道要问什么,而是要获取zone-Id列表,就像这样:

Not 100% sure what you are asking but to get the list of zone-Id would be something like:

var zoneIds = data.Select(z => z.zone_id).Distinct();

这将为您提供各个区域的ID. (不重复项删除重复的区域ID条目.)

This will get you the individual zone ids. (The distinct removes duplicate zone id entries).

如果您想按日期进行过滤,则类似于:

If you want to filter by date it would be something like:

var zoneIds = data.Where(z => z.stime > [lowerDateTimeBound] && z.stime < [upperDateTimeBound]).Select(z => z.zone_id).Distinct();

对于最近的5个,我将使用:

For most recent 5 I would use:

var zoneIds = data.OrderByDescending(z => z.stime).Select(z => z.zone_id).Distinct().Take(5);

如果要获取所有区域而不删除重复项,请删除.Distinct()调用.为了获得更多结果,请更改Take(x)号.结果应如下:

If you want to get all zones without removing duplicates remove the .Distinct() call. And to get more result change the Take(x) number. Result should be as follows:

[1, 2, 3, 4] // With distinct
[1, 1, 2, 2, 3] // Without distinct

更新:根据您的评论.

使用它来获取区域ID的列表:

Use this to get the list of zone Ids:

var zoneIds = data.OrderByDescending(z => z.stime).Select(z => z.zone_id).ToList();
var zoneIdsArray = zoneIds.ToArray();
for(int c = 1; c < zoneIdsArray.Count(); c ++)
{
    if (zoneIdsArray[c].zone_id == zoneIdsArray[c-1].zone_id)
    {
        zoneIds.Remove(zoneIdsArray[c]);
    }
}

var last5Zones = zoneIds.Select(z => z.zone_id).ToList().Take(5);

生成的last5Zones列表应具有最近5个区域的正确列表(根据我认为您从评论中查找的内容)

The resulting last5Zones list should have the correct list of last 5 zones (according to what I think you are looking for from your comments)

这篇关于Linq查询以获取人员当天的访问区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆