如何将项目添加到LINQ重新启动的列表中

时间:2018-04-06 14:07:14

标签: c#

我有以下列表

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

但是我需要手动将“全部”添加到此列表中,我正在尝试这个:

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

但是我收到错误:无法将void分配给隐式类型的局部变量

1 个答案:

答案 0 :(得分:5)

你需要if (args.length > 2 || args.length == 0) { //none or more arguments then supported, some fail System.out.println("Use One or Two Files!"); System.exit(1); } if (args.length == 1) { //got one file or two files //assign first } if (args.length == 2) { //got two files //also got second } 在下一行,你不能用LINQ添加一行:

.Add

问题在于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"}); 会返回.Add,它会尝试将void分配给listOfParks。或者你可以使用.Concat但是为了清楚起见,你没有理由不能在单独的一行上使用它。