简体   繁体   English

使用Linq如何添加列表项

[英]Using Linq how I have to add list items

IEnumerable<ReportFavorite> list = reportService.GetReportFavorites(userId);
ddlReportFavorite.Items.Add()

I don't know how to add the lists to the dropdown using Linq. 我不知道如何使用Linq将列表添加到下拉列表中。 Thanks. 谢谢。

You an use AddRange method: 您使用一个AddRange方法:

 var list = reportService.GetReportFavorites(userId);
 ddlReportFavorite.Items.AddRange(list.ToArray());

Depending on the dropdown control you are using, either of these could work: 根据您使用的下拉控件,以下两种方法都可以起作用:

If it allows its Items to be set to an IEnumerabe<ReportFavourite> : 如果允许其Items设置为IEnumerabe<ReportFavourite>

ddlReportFavorite.Items = reportService.GetReportFavorites(userId);

If Items implements the AddRange method: 如果Items实现AddRange方法:

ddlReportFavorite.Items.AddRange(reportService.GetReportFavorites(userId));

Or, if these fail 或者,如果这些失败

foreach(var reportFavourite in reportService.GetReportFavorites(userId))
    ddlReportFavorite.Items.Add(reportFavourite);

Neither of these methods is really "using LINQ", because LINQ is not a good tool to do this. 这两种方法都不是真正的“使用LINQ”,因为LINQ并不是执行此操作的好工具。 LINQ is meant to be side-effect free. LINQ是无副作用的。

Edit: Your comment suggests that you are using a System.Web.UI.WebControls.DropDownList . 编辑:您的评论表明您正在使用System.Web.UI.WebControls.DropDownList In this case, the Items collection only accepts instances ListItem , so you need to create these from your ReportFavourites. 在这种情况下,Items集合仅接受ListItem实例,因此您需要从ReportFavourites创建它们。 Try 尝试

foreach(var listItem in reportService.GetReportFavorites(userId)
                                     .Select(r => new ListItem(r.Id, r.Name))
    ddlReportFavorite.Items.Add(listItem);

Here, I assume the combo box should display ReportFavourite.Name and have a value of ReportFavourite.Id. 在这里,我假设组合框应显示ReportFavourite.Name并具有ReportFavourite.Id的值。 Use your own properties, of course 当然,使用您自己的属性

或者,如果您已经在方法中检查了数据的完整性,则可以简单地说:

ddlReportFavorite.Items.AddRange(reportService.GetReportFavorites(userId));

Previously I put as IEnumerable. 以前我把它当作IEnumerable。 Now I changed to IList. 现在我更改为IList。 It is working fine now. 现在工作正常。 Thanks to all. 谢谢大家。

    int userId = workContext.CurrentUser.UserID;
    var reportFavoriteList = reportService.GetReportFavorites(userId);
    int count = reportFavoriteList.Count;

    for (int i = 0; i < count; i++)
    {
        ddlReportFavorite.Items.Add(reportFavoriteList[i].FavoriteName);
    }

Since ddlReprotFavorite is an UI control and its Items property represent a set of controls as well you ca n not add directly your business entities instead of use DataSource property which automatically create Items collection from the underlying business entities. 由于ddlReprotFavorite is an UI control and its Items property represent a set of controls as well you ca不能直接添加您的业务实体,而不必使用DataSource属性,该属性会自动从基础业务实体创建Items集合。

IEnumerable<ReportFavorite> list = reportService.GetReportFavorites(userId);      
ddlReportFavorite.DataSource = list;

The IEnumerable<T> is extended by the method Union<T> which unions two IEnumerable<T> 's. IEnumerable<T>由方法Union<T>扩展,该方法将两个IEnumerable<T> This is the more pretty way, without casting it ToList() . 这是更漂亮的方法,无需将其强制转换为ToList()

var reportFavoriteList = reportService.GetReportFavorites(userId);
ddlReportFavorite.Items = ddlReportFavorite.Items.Union(reportFavoriteList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM