无法隐式转换类型System.Linq.IQueryable'到' System.Collections.Generic.List'

时间:2015-03-23 07:47:40

标签: c#

此代码有什么问题?

List<CampaignManager_tbl> candidates = new List<CampaignManager_tbl>();

        candidates = from cat in db.Events_Category_tbl
                     join can in db.Candidates_Info_tbl on cat.events_category_id equals can.events_category_id
                     where cat.events_info_id == events.events_info_id
                     select new CampaignManager_tbl {
                         events_category_name = cat.events_category_name,
                         candidates_fullname = can.candidates_fullname,
                         candidates_place = can.candidates_place
                     };

        return View(candidates);

当我使用此方法仅查询一个表时,它可以工作,但是当我尝试这个时。它不起作用。

1 个答案:

答案 0 :(得分:0)

您必须将IQueryable转换为List。您可以通过在Linq查询末尾添加.ToList()来执行此操作。

List<CampaignManager_tbl> candidates = new List<CampaignManager_tbl>();

candidates = (from cat in db.Events_Category_tbl
                  join can in db.Candidates_Info_tbl on cat.events_category_id equals can.events_category_id
                  where cat.events_info_id == events.events_info_id
                  select new CampaignManager_tbl {
                         events_category_name = cat.events_category_name,
                         candidates_fullname = can.candidates_fullname,
                         candidates_place = can.candidates_place
                  }).ToList();

return View(candidates);