格式列表连接字段

发布于 2024-10-31 07:19:07 字数 1118 浏览 1 评论 0原文

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

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2024-11-07 07:19:07

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

希望我没有误解你的问题,但你可以通过在返回值中添加分隔符来添加这样做查询:

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

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

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

苍暮颜 2024-11-07 07:19:07
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();             
    }
}

仅供参考

您还可以使用 String.Join(string, IEnumerable) 来获得如下结果:

IEnumerable<string> GetCitiesInCountryWithState(string)

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

string s = String.Join("; ", r);
// "Miami, Florida; Key West, Florida"
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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文