user1537319
user1537319

Reputation:

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. Thanks.

Upvotes: 0

Views: 5931

Answers (6)

Jan P.
Jan P.

Reputation: 3297

The IEnumerable<T> is extended by the method Union<T> which unions two IEnumerable<T>'s. This is the more pretty way, without casting it ToList().

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

Upvotes: 0

user1537319
user1537319

Reputation:

Previously I put as IEnumerable. Now I changed to 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);
    }

Upvotes: 1

Jens
Jens

Reputation: 25563

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>:

ddlReportFavorite.Items = reportService.GetReportFavorites(userId);

If Items implements the AddRange method:

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 is meant to be side-effect free.

Edit: Your comment suggests that you are using a System.Web.UI.WebControls.DropDownList. In this case, the Items collection only accepts instances ListItem, so you need to create these from your 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. Use your own properties, of course

Upvotes: 1

sll
sll

Reputation: 62504

Since ddlReprotFavorite is an UI control and itsItemsproperty represent a set of controls as well you can not add directly your business entities instead of use DataSource property which automatically create Items collection from the underlying business entities.

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

Upvotes: 0

Vee
Vee

Reputation: 23

Or if you've already checked the data's integrity in the method, you could just simply say:

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

Upvotes: 1

cuongle
cuongle

Reputation: 75306

You an use AddRange method:

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

Upvotes: 3

Related Questions