开发者

Format List<T> to concatenate fields

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 citi开发者_如何学运维es.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.


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 :

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


public static List<string> GetCitiesInCountryWithState(string isoalpha2)
{
    using (var context= new atomicEntities())
    {
        return (from city in context.Cities
                join country in context.Countries on c.CountryId equals country.CountryId
                where country.IsoAlpha2 == isoalpha2
                select String.Concat(city.CityName, ", ", city.State)
                // select String.Format("{0}, {1}", city.CityName, city.State)
                ).ToList();             
    }
}

FYI

You can also use String.Join(string, IEnumerable<String>) to get a result like this:

IEnumerable<string> GetCitiesInCountryWithState(string)

IEnumerable<string> r = GetCitiesInCountryWithState("100");
// { { "Miami, Florida" }, { "Key West, Florida" } }

string s = String.Join("; ", r);
// "Miami, Florida; Key West, Florida"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜