简体   繁体   English

Linq查询以选择不同的记录并计算不同的记录数

[英]Linq query to select distinct record and count number of distinct record

I have table tblVisitor which stores visitors location details. 我有tblVisitor表,该表存储访问者的位置详细信息。 It has below structure. 它具有以下结构。

vRegion      NVARCHAR (100) NULL,
vTimeZone    NVARCHAR (100) NULL,
vLat         NVARCHAR (MAX) NULL, //Latitude
vLong        NVARCHAR (MAX) NULL, //Longitude

Now, this storage depends on each session and when the session expires the same location details might be stored again. 现在,此存储取决于每个session并且当session到期时,可能会再次存储相同的位置详细信息。 Now I am trying to display this in a Map and thus am getting Latitude and Longitude details in a model class. 现在,我试图在地图中显示它,从而在模型类中获取纬度和经度详细信息。 My model class is as below: 我的模型课如下:

public class VisitorMapViewModel
{
    public int count { get; set;}
    public GeoCoordinate coords { get; set; }
}

Am trying to fill model as below, but with no luck. 我正在尝试按以下方式填充模型,但是没有运气。

List<VisitorMapViewModel> model = new List<VisitorMapViewModel>();
var data = _db.tblVisitors.Distinct().ToList().Select(v => new VisitorMapViewModel()
            {
                coords = new System.Device.Location.GeoCoordinate(Convert.ToDouble(v.vLat), Convert.ToDouble(v.vLong)),
                count = _db.tblVisitors.GroupBy(m => new { m.vLat,m.vLong}).Select(m=>m).Distinct().Count()
            }).ToList();

There are 2 problems here.. Converting Latitude and Longitude values of type nvarchar to double and then getting distinct values of latitude and longitude and counting the number of occurances.. The above query keeps hitting one or other kind of exception when I interchange converting to ToList() before Distinct() and vice versa.. I feel am almost near, but not sure what am missing. 这里有2个问题。.将nvarchar类型的LatitudeLongitude值转换为double nvarchar ,然后获取不同的纬度和经度值并计算发生的次数。.当我互换转换为ToList()Distinct() ToList()之前,反之亦然。.我感觉差不多了,但是不确定丢失了什么。 Could anyone help on this please. 任何人都可以帮忙。

You should do one query that groups on the latitude and longitude and gets the counts. 您应该执行一个查询,将其按纬度和经度分组并获取计数。 Then convert the results into your model. 然后将结果转换为模型。 You can use AsEnumerable when you want to switch from SQL queries to in memory Linq queries without needing the overhead of creating a list. 想要从SQL查询切换到内存Linq查询时,可以使用AsEnumerable ,而无需创建列表的开销。

var data = (from visitor in _db.tblVisitors
            group visitor by new { visitor.vLat, visitor.vLong } into grp
            select new 
            {
                grp.Key.vLat,
                grp.Key.vLong,
                Count = grp.Count()
            })
    .AsEnumerable()
    .Select(x => new new VisitorMapViewModel()
        {
            coords = new System.Device.Location.GeoCoordinate(
                double.Parse(x.vLat), 
                double.Parse(x.vLong)),
            count = x.Count
        })
    .ToList();

Alternatively you could do this all in method syntax 或者,您可以使用方法语法来完成所有这些操作

var data = _db.tblVisitors
    .GroupBy(v => new { v.vLat, v.vLong })
    .Select(grp => new 
        {
            grp.Key.vLat,
            grp.Key.vLong,
            Count = grp.Count()
        })
    .AsEnumerable()
    .Select(x => new new VisitorMapViewModel()
        {
            coords = new System.Device.Location.GeoCoordinate(
                double.Parse(x.vLat), 
                double.Parse(x.vLong)),
            count = x.Count
        })
    .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM