格式列表连接字段 [英] Format List<T> to concatenate fields

查看:49
本文介绍了格式列表连接字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了数百个与被问到的问题类似的问题,但没有找到我的答案.如果以前曾问过这个问题,请接受我的道歉.

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.

我有一个SQL数据库,分别包含一列城市和一列州.我使用实体框架作为DAL,并且在BLL中有很多LINQ可以格式化,移动内容等.我在UI上有一个下拉列表,该下拉列表目前接受方法中的 ToList()在我的BLL中,并且一切正常,但是我需要更改此方法以返回城市和州,并以逗号分隔.我到目前为止(这行不通!):

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();  
    }
}

我已经尝试了许多LINQ变体,但是显然我没有弄错这一点.任何人都可以提供一些帮助吗?一如既往,不胜感激:)

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 :)

编辑1 :我希望该方法的输出为连接的城市名称和州的列表,并用逗号和空格隔开;例如:

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

在我的示例中,输入只是有关国家的ISO-Alpha2代码,例如:"US".例如,对于英国来说,这可以是"GB",对于法国来说,可以是"FR",对于德国来说,可以是"DE".

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.

推荐答案

编辑:已删除匿名类型在定界符声明中添加了一些空格:-),"

Removed anonymous type 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 :

public static List<string> 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 c.CityName + delimiter + c.State ;            
        return query.ToList();
    }
}

这将创建一个字符串类型的列表

This will create a list of type string

这篇关于格式列表连接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆