使用 LINQ for Dropdown 返回串联字符串
这是这个问题的后续内容: 格式列表List
,以便可以重新使用它。我到目前为止,但这不正确(因为我知道 Iqueryable
不正确,它只是防止 AnonymousType
错误):
public static IQueryable GetCitiesInCountryWithState(string isoalpha2)
{
const string delimiter = ", ";
using (var ctx = new atomicEntities())
{
var query = (from c in ctx.Cities
join ctry in ctx.Countries on c.CountryId equals ctry.CountryId
where ctry.IsoAlpha2 == isoalpha2
select new
{
CityId = c.CountryId,
CityName = c.CityName + delimiter + c.State
});
return query;
}
}
我希望能够返回 < code>List
ddlCity.DataSource = GetCitiesInCountryWithState(Session["BusinessCountry"].ToString());
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();
我尝试了各种方法,但没有运气。我知道我已经很接近了 - 但需要帮助!帮助表示赞赏:)
This is follow on from this question: Format List<T> to concatenate fields. The answer was correct, but post-answer I wanted to return the List
from the method so it could be re-used. I got this far, but this is not correct (because I know Iqueryable
is not correct, it simply prevents the AnonymousType
error):
public static IQueryable GetCitiesInCountryWithState(string isoalpha2)
{
const string delimiter = ", ";
using (var ctx = new atomicEntities())
{
var query = (from c in ctx.Cities
join ctry in ctx.Countries on c.CountryId equals ctry.CountryId
where ctry.IsoAlpha2 == isoalpha2
select new
{
CityId = c.CountryId,
CityName = c.CityName + delimiter + c.State
});
return query;
}
}
I want to be able to return a List<string>
here (if possible) and then do something like this on the UI:
ddlCity.DataSource = GetCitiesInCountryWithState(Session["BusinessCountry"].ToString());
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();
I've tried all sorts, but without luck. I know I've been close - but help is needed! Help appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法返回匿名类型,编译器无法维护类型安全。
所以如果你定义一个类
将您的 select new {} 替换为 select new City{} 并返回列表;或 IQueryable<城市>你应该没事的。
You cannot return an Anonymous Type, the compiler can not maintain type safety.
So if you define a class
Replace your select new {} with select new City{} and return a List<City> or IQueryable<City> you should be fine.
您可能会考虑以下一个选项:
在您的 City 实体上,我假设您有 EDM 工具生成的部分类的一部分,并且您可能有部分类的另一部分,其中包含的代码不是来自EDM 一代。
在分部类的非生成部分中,您可以添加一个瞬态属性,该属性将 CityName 和 State 作为只读字符串提供(见下文)。瞬态意味着该属性不会持久化到数据库中,并且通常是从某些现有字段生成的。此技术在 ViewModel 中大量使用,为视图提供更多 UI 友好的属性。
然后,您可以将其用作绑定中 City 对象的 DataTextField。如果您走这条路线,我认为您不需要在查询中使用匿名类型 - 您可以按原样返回 City 对象。与从查询中返回自定义对象相比,这样做的好处是,如果在类上定义了该属性,您将始终拥有它,并且无论您使用什么查询来检索城市,它都将始终以相同的方式工作。
希望这是有道理的...我对您的项目的设置方式做了一些假设。
Here is an option that you might look into:
On your City entity, I'm assuming you have a portion of a partial class generated by the EDM tools, and you may have another portion of the partial class which contains code that is not from the EDM generation.
In the non-generated portion of the partial class, you can add a transient property that gives the CityName and State as a readonly string (see below). Transient means that the property is not persisted to the database, and is usually generated from some existing fields. This technique is used a lot in ViewModels to provide more UI-friendly properties to a view.
You can then use this as the DataTextField for your City object in the binding. If you go this route, I don't think you'll need an anonymous type in the query - you can just return the City object as is. The benefit of doing it this way vs. returning a custom object from the query is that if the property is defined on the class, you'll always have it, and it will always work the same no matter what query you use to retrieve Cities.
Hope that makes sense... I made some assumptions on how your project is setup.
接受克里斯所说的话来为你扩展。
{
}
进而
To expand for you taking what Chris said.
{
}
and then