add nested group by query Linq to property model class ASP.NET

I have struggle with adding the grouped data table with linq query to model property.

Here’s my model

    public class Ports
        {
            public String city { get; set; }
        }

and here’s my controller

    public ActionResult ShipSchedule()
            {
                DatabaseContext db = new DatabaseContext();
                var Ports = new Ports();
                Ports.city = from m in db.ports_data group m by new { m.city } into n select new { n.Key.city }; 
                return View();
            }

I’ve replaced the model part to List<> and replaced again to another, but this part

    Ports.city = (from m in db.ports_data group m by new { m.city } into n select new { newcity = n.Key.city }).ToList(); 

is still tell me that part cannot convert type 'system.collections.generic.list<<anonymous type: string newcity>>' to 'system.collections.generic.list<string>'

Did anyone know the correct ways from this?

and one more thing, I want to display that part to my view in dropdown, if anyone have a better ways

here’s my view

@model example.Models.Ports
@{
   List<SelectListItem> listItems = new List<SelectListItem>();
   foreach (var mod in Model.city)
   {
                                        
       listItems.Add(new SelectListItem
       {
           Text = mod.ToString(),
           Value = mod.ToString()
       });
   }
                                    
}

@Html.DropDownListFor(model => model.selected, listItems, "-- Select item --")

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

the error is telling you exactly what is wrong, you have a string but are trying to assign a list to it (two very different objects), you can simply:

 public class Ports
    {
        public List<String> cities { get; set; }
    }

which will make that string be valid, but a better practice would be make “Ports” a list and use it as List<Port> and each port will contain “city” (and all the other port properties).

but in any case to get all ports from the database you probably can do:

var ports = db.ports_data.ToList();

that should give you a list with all the ports in your database (no need to create that New Ports() and populate it, if you need to group by city it should be as easy as:

var ports = db.ports_data.GroupBy(x=>x.city).ToList();

in the comment you mentioned that you are getting duplicates, for that you may need distinct, but if you don’t need duplicates you shouldn’t be adding duplicates to the database to start with, so we would be touching the wrong end here, but in any case I believe you need Distinct() if combined with a select you will get only cities, different ones if there is duplicates:

var portsCities = db.ports_data.Select(x=>x.city).Distinct().ToList();

that will result in a List<string> of cities, all different

as you can see Linq have many ways to get exactly the data you need, I would play around with it and experiment, best way to learn!

I would also try to use LINQ method (as the examples above) if you are starting, it is easier to manage (more object oriented) you can see some examples here


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x