0

I have the following problem: the query with linq works ok, but I need to add the coordinates within an array for each record that is repeated

Model

    public class AppPoisModel
    {
        public int idPoiType { get; set; }
        public string sector { get; set; }
        public double latPoint { get; set; }
        public double lngPoint { get; set; }
    }

Query

var result = (from c in db.coord
                      select new AppPoisModel
                      {
                          idPoiType = c.id,
                          sector = c.sector,
                          latPoint = c.latitude ?? 0,
                          lngPoint = c.longitude ?? 0
                      }).ToList();

Result

[
{
    "$id": "1",
    "sector" : "A",
    "latPoint": 0,
    "lngPoint": 0
},
{
    "$id": "2",
    "sector" : "A",
    "latPoint": 0,
    "lngPoint": 0
}
]

Desired result

[
{
    "$id": "1",
    "sector" : "A",
    "coords": [{latPoint, lngPoint}, {latPoint, lngPoint}]
}
]

thank you very much for your contributions

Gabriel Sule
  • 373
  • 6
  • 17
  • 2
    You didn´t provide any data. so it´s hard to guess how you want to get the result from that data. – MakePeaceGreatAgain Apr 20 '18 at 14:51
  • If you group on sector `A`, why still show an ID value for one of the grouped entries? You're inherently going to hide any additional ID, rendering the ID property's functional value useless. – Flater Apr 20 '18 at 14:52
  • @HimBromBeere: What information are you missing? The select statement in **query** seems to convey the needed information for the four relevant fields. – Flater Apr 20 '18 at 14:53
  • Do you have to use LINQ for this operation? If you do not take it seriously the query will be translated into nested SQL statements which would increase the complexity and execution time. I would choose fetching the resultset with EF or ADO.Net, then execute the grouping LINQ functions. – ali Apr 20 '18 at 15:07

2 Answers2

0

The initial query you suggested has no clue that there's a common relationship with items in the same sector.

To accomplish this, you'll want to use the Enumerable.GroupBy() method to group those items together with the basis of having the same id and sector together.

If both of those will always be correlated, you can just GroupBy() one of them to make the comparison simpler, but the results will also reflect that.

var result = (from c in db.coord
    select new AppPoisModel
    {
        idPoiType = c.id,
        sector = c.sector,
        latPoint = c.latitude ?? 0,
        lngPoint = c.longitude ?? 0
    }).GroupBy(x => new { id = x.idPoiType, sector = x.sector });

In your case, possibly with both id and sector. This will be your key when you want to loop over the results. So that you can morph the results into the data type you want.

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
  • Where are you getting `x.Field<>()` from? Type `AppPoisModel` simply has public properties. Secondly, you can't group on ID, as the ID for the two POIs (in the example data) is different. – Flater Apr 20 '18 at 15:03
  • You're right, I had pasted some of this from another example I had. It can be simpler. – Sunny Patel Apr 20 '18 at 15:05
0

looks like you need a group by...

Also you might create a class for Coords, and make AppPoisModel or a new result class, with a coords field typed as a collection of coords

check it out: Group by in LINQ similar solution https://stackoverflow.com/a/47580961

rsb55
  • 66
  • 1
  • 6
  • 1
    Linking to a different question (without directly answering the question) is not a good approach. Links can be lost, rendering this answer useless in the future. Add the relevant parts directly to your answer with an explantion (note: you can keep the link as an _additional_ reference) – Flater Apr 20 '18 at 15:05
  • I like the answer. It explains directly the parts and adds two links as an additional reference. Of course a line of code would always be fine ;-) – Xan-Kun Clark-Davis Apr 20 '18 at 15:09