Search code examples
c#asp.net-mvc-3model-view-controllerdropdownbox

populating selectList in C# issue


So i have this code,

var CityName = (from ct in db.ZipCodes orderby ct.CityName
                       where (ct.StateName.Equals(StateName))
                       select ct.CityName).ToList().Distinct();
SelectList city = new SelectList(CityName);
ViewBag.City = city;

which works perfectly fine, Now i needed some more conditions which fetch the correct result from the database. the query for that is this

select CityName from ZipCodes where StateName='New York' and CityType='D' group by CityName,AreaCode

now anyone please tell me how should i use the group by in this code, i tried but it gives me syntax error.

Also i tried creating a Stored Procedure

var CityName= db.sp_get_CityName(StateName).ToList();

but it populates the sp_get_citynameresult in drop down.

How can I solve this issue?


Solution

  • you have to do like this

    var Result =   (from ct in db.ZipCodes
                    where ct.StateName == "New York" && ct.CityType == "D"
                    group ct by new { ct.CityName,ct.AreaCode }into g
                    select new {CityName = g.Key.CityName,AreaCode = g.Key.AreaCode,g.ToList()};