papagallo
papagallo

Reputation: 155

how to add item to list retrived by LINQ

I have the following list

 var listOfParks = (from s in DB.MasterDatas

                           select new SelectListItem
                           {
                               Text = s.service_under,

                           }).Distinct().Union(
                           from t in DB.MasterDatas
                            join f in DB.Users1
                           on t.pv_person_resp_id equals f.user_id     
                           select new SelectListItem
                           {
                               Text = f.user_name

                           }).ToList()

But I need to add manually "All" to this list,i am trying this:

    var listOfParks = (from s in DB.MasterDatas

                           select new SelectListItem
                           {
                               Text = s.service_under,

                           }).Distinct().Union(
                           from t in DB.MasterDatas
                            join f in DB.Users1
                           on t.pv_person_resp_id equals f.user_id     
                           select new SelectListItem
                           {
                               Text = f.user_name

                           }).ToList().Add(new SelectListItem{Text="All"});

but I get error:Cannot assign void to an implicitly-typed local variable

Upvotes: 1

Views: 64

Answers (1)

Ron Beyer
Ron Beyer

Reputation: 11273

You need to .Add it on the next line, you can't do an add in one line with LINQ:

var listOfParks = (from s in DB.MasterDatas

                           select new SelectListItem
                           {
                               Text = s.service_under,

                           }).Distinct().Union(
                           from t in DB.MasterDatas
                            join f in DB.Users1
                           on t.pv_person_resp_id equals f.user_id     
                           select new SelectListItem
                           {
                               Text = f.user_name

                           }).ToList();
listOfParks.Add(new SelectListItem{Text="All"});

The issue is that .Add returns void which it is trying to assign to listOfParks which it cannot. Or you can use .Concat but really there is no reason you can't have it on a separate line for clarity.

Upvotes: 6

Related Questions