格式列表连接字段
我已经查看了数百个与所问问题类似的问题,但还没有找到我的答案。如果以前曾问过这个问题,请接受我的歉意。
我有一个 SQL 数据库,其中分别包含一列城市和一列其州。我使用实体框架作为我的 DAL,并且在我的 BLL 中有很多用于格式化、移动内容等的 LINQ。我的 UI 上有一个下拉菜单,当前从方法接受 ToList()
在我的 BLL 中,一切正常,但我需要更改此方法以返回城市和州,并用逗号分隔。到目前为止我有这个(这不起作用!):
public static List<char> 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 { c.CityName, c.State };
var cities = query.Select(i => i.CityName).Aggregate((i, j) => i + delimiter + j);
return cities.ToList();
}
}
我已经尝试了许多 LINQ 变体,但显然我没有得到正确的结果。有人可以提供一些帮助吗?一如既往,我们很感激:)
编辑1:我希望该方法的输出是由逗号和空格分隔的串联城市名称和州的列表;例如:
Miami, Florida
Key West, Florida
New York, New York
Boston, Massachusetts
输入只是相关国家/地区的 ISO-Alpha2 代码,在我的示例中为:“US”。例如,英国可以是“GB”,法国可以是“FR”,德国可以是“DE”。
I've looked at hundreds of similar questions on SO to the one asked, but haven't found my answer. If this has been asked before, please accept my apologies.
I have a SQL database with a column of cities and a column of their states respectively. I use the Entity Framework as my DAL and in my BLL have lots of LINQ to format, move stuff, etc. I have on my UI a drop-down which currently accepts a ToList()
from a method in my BLL and all works well, but I need to alter this method to return city and state, separated by a comma. I have this so-far (which doesn't work!):
public static List<char> 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 { c.CityName, c.State };
var cities = query.Select(i => i.CityName).Aggregate((i, j) => i + delimiter + j);
return cities.ToList();
}
}
I've tried numerous LINQ variations, but I'm obviously not getting this correct. Can anyone offer some help please? As always, it is appreciated :)
EDIT 1: I'd like the output of the method to be a list of concatenated City names and states separated by a comma and a space; for example:
Miami, Florida
Key West, Florida
New York, New York
Boston, Massachusetts
Input is simply the ISO-Alpha2 code for the country in question, in the case of my example: 'US'. This could be 'GB' for Great Britain or 'FR' for France or 'DE' for Germany for instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:删除匿名类型
编辑:在分隔符声明中添加空格有点不同:-)“,”
希望我没有误解你的问题,但你可以通过在返回值中添加分隔符来添加这样做查询:
这将创建一个字符串类型的列表
Edit: Removed anonymous type
Edit: Added space in delimiter declaration to be a bit different :-) ", "
Hopefully I am not misunderstanding your question but you can add do it as follows by adding the delimiter in the return value of the query :
This will create a list of type string
仅供参考
您还可以使用 String.Join(string, IEnumerable) 来获得如下结果:
FYI
You can also use
String.Join(string, IEnumerable<String>)
to get a result like this: