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

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

问题描述

我已经看到了数以百计的关于这个问题的类似问题,但没有找到我的答案。如果以前有人问过,请接受我的歉意。

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变体, m显然没有得到这个正确。任何人都可以提供一些帮助吗?一如以往,请注意:)

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代码,在这种情况下我的例子:美国。这可能是英国的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.

推荐答案

strong>编辑:删除匿名类型
编辑:添加分隔符声明中的空格有点不同:-),

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

这将创建一个类型为string的列表

This will create a list of type string

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

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