所以我有这段代码,
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;
工作得很好,现在我需要一些从数据库中获取正确结果的条件。对此的查询是
select CityName from ZipCodes where StateName='New York' and CityType='D' group by CityName,AreaCode
现在有人请告诉我如何在此代码中使用该组,我试过但它给了我语法错误。
我也尝试创建一个存储过程
var CityName= db.sp_get_CityName(StateName).ToList();
但它会在下拉列表中填充sp_get_citynameresult。
我该如何解决这个问题?
答案 0 :(得分:1)
您可以使用下面提到的代码。
var CityName=ZipCodes.Where(p=>p.StateName="New York" && CityType="D").GroupBy(q => new { q.AreaCode,q.CityName});
答案 1 :(得分:1)
你必须这样做
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()};