使用 LINQ for Dropdown 返回串联字符串

发布于 2024-10-31 19:40:54 字数 1321 浏览 2 评论 0原文

这是这个问题的后续内容: 格式列表连接字段。答案是正确的,但在回答后我想从该方法返回 List ,以便可以重新使用它。我到目前为止,但这不正确(因为我知道 Iqueryable 不正确,它只是防止 AnonymousType 错误):

public static IQueryable 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
                                   {
                                       CityId = c.CountryId,
                                       CityName = c.CityName + delimiter + c.State
                                   });
            return query;
        }
    }

我希望能够返回 < code>List此处(如果可能),然后在用户界面上执行类似的操作:

ddlCity.DataSource = GetCitiesInCountryWithState(Session["BusinessCountry"].ToString());
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

我尝试了各种方法,但没有运气。我知道我已经很接近了 - 但需要帮助!帮助表示赞赏:)

This is follow on from this question: Format List<T> to concatenate fields. The answer was correct, but post-answer I wanted to return the List from the method so it could be re-used. I got this far, but this is not correct (because I know Iqueryable is not correct, it simply prevents the AnonymousType error):

public static IQueryable 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
                                   {
                                       CityId = c.CountryId,
                                       CityName = c.CityName + delimiter + c.State
                                   });
            return query;
        }
    }

I want to be able to return a List<string> here (if possible) and then do something like this on the UI:

ddlCity.DataSource = GetCitiesInCountryWithState(Session["BusinessCountry"].ToString());
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

I've tried all sorts, but without luck. I know I've been close - but help is needed! Help appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

揽清风入怀 2024-11-07 19:40:54

您无法返回匿名类型,编译器无法维护类型安全。

所以如果你定义一个类

class City {
  public int CityId {get; set;}
  public string CityName {get; set;}
}

将您的 select new {} 替换为 select new City{} 并返回列表;或 IQueryable<城市>你应该没事的。

You cannot return an Anonymous Type, the compiler can not maintain type safety.

So if you define a class

class City {
  public int CityId {get; set;}
  public string CityName {get; set;}
}

Replace your select new {} with select new City{} and return a List<City> or IQueryable<City> you should be fine.

安静 2024-11-07 19:40:54

您可能会考虑以下一个选项:

在您的 City 实体上,我假设您有 EDM 工具生成的部分类的一部分,并且您可能有部分类的另一部分,其中包含的代码不是来自EDM 一代。

在分部类的非生成部分中,您可以添加一个瞬态属性,该属性将 CityName 和 State 作为只读字符串提供(见下文)。瞬态意味着该属性不会持久化到数据库中,并且通常是从某些现有字段生成的。此技术在 ViewModel 中大量使用,为视图提供更多 UI 友好的属性。

public partial class City
{
    public string CityNameAndState
    {
        get
        {
            return CityName + delimiter + State;
        }
    }
}

然后,您可以将其用作绑定中 City 对象的 DataTextField。如果您走这条路线,我认为您不需要在查询中使用匿名类型 - 您可以按原样返回 City 对象。与从查询中返回自定义对象相比,这样做的好处是,如果在类上定义了该属性,您将始终拥有它,并且无论您使用什么查询来检索城市,它都将始终以相同的方式工作。

希望这是有道理的...我对您的项目的设置方式做了一些假设。

Here is an option that you might look into:

On your City entity, I'm assuming you have a portion of a partial class generated by the EDM tools, and you may have another portion of the partial class which contains code that is not from the EDM generation.

In the non-generated portion of the partial class, you can add a transient property that gives the CityName and State as a readonly string (see below). Transient means that the property is not persisted to the database, and is usually generated from some existing fields. This technique is used a lot in ViewModels to provide more UI-friendly properties to a view.

public partial class City
{
    public string CityNameAndState
    {
        get
        {
            return CityName + delimiter + State;
        }
    }
}

You can then use this as the DataTextField for your City object in the binding. If you go this route, I don't think you'll need an anonymous type in the query - you can just return the City object as is. The benefit of doing it this way vs. returning a custom object from the query is that if the property is defined on the class, you'll always have it, and it will always work the same no matter what query you use to retrieve Cities.

Hope that makes sense... I made some assumptions on how your project is setup.

岁月流歌 2024-11-07 19:40:54

接受克里斯所说的话来为你扩展。

class City 

{

   public int CityId {get; set;}
   public string CityName {get; set;}

}

进而

public static List<City> 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 City
                               {
                                   CityId = c.CountryId,
                                   CityName = c.CityName + delimiter + c.State
                               }).ToList();
        return query;
    }
}


var cityList = GetCitiesInCountryWithState(Session["BusinessCountry"].ToString());
ddlCity.DataSource = cityList;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();

To expand for you taking what Chris said.

class City 

{

   public int CityId {get; set;}
   public string CityName {get; set;}

}

and then

public static List<City> 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 City
                               {
                                   CityId = c.CountryId,
                                   CityName = c.CityName + delimiter + c.State
                               }).ToList();
        return query;
    }
}


var cityList = GetCitiesInCountryWithState(Session["BusinessCountry"].ToString());
ddlCity.DataSource = cityList;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文