使用linq中的表加入列表

时间:2018-03-19 13:04:14

标签: c# linq lambda ef-code-first

我有问题。我获取var类型变量中的所有数据,然后想要在代码优先方法中应用数据库表的连接。面对问题,在互联网上进行大量搜索并申请但失败。

$sql = "SELECT accountNumber, Name,studentManager,contract,
            nationality,university,major,Course,specialNotes,
            Phone,email,birthday,uniAddress 
        FROM " . $dbname . " 
        WHERE id ='$id'";

$query2 = mysqli_query($conn, $sql);
$thisArray = mysqli_fetch_all($query2, MYSQLI_NUM);


header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=userData.csv");
$fp = fopen('php://output', 'w');
foreach ($thisArray[0] as $row3) {
    fputcsv($fp,$row3);
}

fclose($fp);

现在我希望var joinedData = from menuGroup in _menuGroupMenusRepository.GetAll() .Where(x => x.GroupId == input.GroupId) join menus in _menuRepository.GetAll() on menuGroup.MenuId equals menus.Id join categSubcateg in _menuCategSubCategRepository.GetAll() on menus.Id equals categSubcateg.MenuId join categ in _menuCategoryRepository.GetAll() on categSubcateg.CategoryId equals categ.Id select new { CategoryId = categSubcateg.CategoryId, CategoryName = categ.Category, }; 变量加入joinedData表。 MainMenuSort表还有MainMenuSortgroupid

1 个答案:

答案 0 :(得分:3)

执行加入,您只需执行以下操作

 var q=(from jd in joinedData 
         join mms in dataContext.MainMenuSort 
         on jd.CategoryId equals mms.CategoryId 
 select jd).ToList(); 

如果它的数据表那么

 var q=(from jd in joinedData 
         join mms in dtMainMenuSort.AsEnumerable() 
         on jd.CategoryId equals mms.Field<int>("CategoryId")
 select jd).ToList();