【问题标题】:populating selectList in C# issue在 C# 问题中填充 selectList
【发布时间】:2014-06-05 18:44:45
【问题描述】:

所以我有这个代码,

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

现在有人请告诉我应该如何在这段代码中使用 group by,我试过了,但它给了我语法错误。

我也尝试创建存储过程

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

但它会在下拉列表中填充 sp_get_citynameresult。

我该如何解决这个问题?

【问题讨论】:

    标签: c# asp.net-mvc-3 model-view-controller dropdownbox


    【解决方案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()};
    

    【讨论】:

    • var CityName1 = (from ct in db.ZipCodes where ct.StateName == StateName && ct.CityType == "D" group ct by new { ct.CityName,ct.AreaCode }into g select new {g.Key.CityName}).ToList(); 成功了
    【解决方案2】:

    您可以使用下面提到的代码。

    var CityName=ZipCodes.Where(p=>p.StateName="New York" && CityType="D").GroupBy(q => new { q.AreaCode,q.CityName});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      相关资源
      最近更新 更多