.net mvc 中如何多表查询数据

qq_23172683 2016-02-14 10:39:02
   Strong.IBLL.ISysUserService _userService = new SysUserService();
public ActionResult GetAllUserInfos()
{
int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);
var userInfo = new UserInfoQuery()
{
PageIndex = pageIndex,
PageSize = pageSize,
Total = 0,
IsEanble = true
};
var data = from u in _userService.LoadSearchData(userInfo)
select new
{
u.UserId,
u.UserName,
u.PassWord,
u.CreateDate,
u.CreatePerson,
u.UpdateDate,
u.UpdatePerson,
u.IsEnable,
u.SortNum,
u.Description
};

var result = new { total = userInfo.Total, rows = data };
return Json(result);
}

这样查询的是一个用户表,但是我还想和角色表关联起来请问如何去写呢?
...全文
3914 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_15148111 2017-10-14
  • 打赏
  • 举报
回复
db.用户表.AsEnumerable().join(db.角色表,a=>a.角色编号,b=>b.角色编号,(a,b)=>new{a,b}).select(item=>new{ jueId=item.b.角色编号 }).tolist();
hsl_44 2016-03-08
  • 打赏
  • 举报
回复
都没有用Linq和Lamda表达式写的吗?
threenewbee 2016-02-17
  • 打赏
  • 举报
回复
引用 6 楼 qq_23172683 的回复:
[quote=引用 2 楼 q107770540 的回复:]
 var data = from u in _userService.LoadSearchData(userInfo)
            join r in _userService.Roles on u.roleId equals r.Id
                       select new
                       {
                           r.RoleName,
                           u.UserId,
                           u.UserName,
                           u.PassWord,
                           u.CreateDate,
                           u.CreatePerson,
                           u.UpdateDate,
                           u.UpdatePerson,
                           u.IsEnable,
                           u.SortNum,
                           u.Description
                       };
 
谢谢了,解决了我的问题[/quote] 督察的答案完全正确。解决了就及时结贴吧。
qq_23172683 2016-02-17
  • 打赏
  • 举报
回复
引用 2 楼 q107770540 的回复:
 var data = from u in _userService.LoadSearchData(userInfo)
            join r in _userService.Roles on u.roleId equals r.Id
                       select new
                       {
                           r.RoleName,
                           u.UserId,
                           u.UserName,
                           u.PassWord,
                           u.CreateDate,
                           u.CreatePerson,
                           u.UpdateDate,
                           u.UpdatePerson,
                           u.IsEnable,
                           u.SortNum,
                           u.Description
                       };
 
谢谢了,解决了我的问题
xiaoqiu_net 2016-02-16
  • 打赏
  • 举报
回复
linq多表啊, form x in xtab join y in ytab select new { 取属性(x.xxxx,y.xxx) }
蝶恋花雨 2016-02-16
  • 打赏
  • 举报
回复
var data = (from u in _userService.LoadSearchData(userInfo)
            join r in _userService.Roles on u.roleId equals r.Id
                       select new UserInfo
                       {
                           r.RoleName,
                           u.UserId,
                           u.SortNum,
                           u.Description
                       }).ToList<UserInfo>();
                       
                       public class UserInfo
                      {
                        public string RoleName{get;set;}
                        /////
                      }
蝶恋花雨 2016-02-16
  • 打赏
  • 举报
回复
var data = (from u in _userService.LoadSearchData(userInfo)
            join r in _userService.Roles on u.roleId equals r.Id
                       select new UserInfo
                       {
                          RoleName= r.RoleName,
                          UserId= u.UserId,
                           SortNum=u.SortNum,
                          Description= u.Description
                       }).ToList<UserInfo>();
                       
                       public class UserInfo
                      {
                        public string RoleName{get;set;}
                        /////
                      }
q107770540 2016-02-16
  • 打赏
  • 举报
回复
 var data = from u in _userService.LoadSearchData(userInfo)
            join r in _userService.Roles on u.roleId equals r.Id
                       select new
                       {
                           r.RoleName,
                           u.UserId,
                           u.UserName,
                           u.PassWord,
                           u.CreateDate,
                           u.CreatePerson,
                           u.UpdateDate,
                           u.UpdatePerson,
                           u.IsEnable,
                           u.SortNum,
                           u.Description
                       };
 
Justin-Liu 2016-02-15
  • 打赏
  • 举报
回复
查查linq多表查询
An outstanding author team presents the ultimate Wrox guide to ASP.NET MVC 4 Microsoft insiders join giants of the software development community to offer this in-depth guide to ASP.NET MVC, an essential web development technology. Experienced .NET and ASP.NET developers will find all the important information they need to build dynamic, data-driven websites with ASP.NET and the newest release of Microsoft's Model-View-Controller technology. Featuring step-by-step guidance and lots of code samples, this guide gets you started and moves all the way to advanced topics, using plenty of examples. Designed to give experienced .NET and ASP.NET programmers everything needed to work with the newest version of MVC technology Expert author team includes Microsoft ASP.NET MVC insiders as well as leaders of the programming community Covers controllers, views, models, forms and HTML helpers, data annotation and validation, membership, authorization, security, and routing Includes essential topics such as Ajax and jQuery, NuGet, dependency injection, unit testing, extending MVC, and Razor Includes additional real-world coverage requested by readers of the previous edition as well as a new case study example chapter 下面是上一版书的文介绍 《ASP.NET MVC 3 高级编程》主要内容是描述视图的概念,探讨Razor语法、NuGet、单元测试等,解释控制器在MVC框架的作用,以及模型在绑定和数据访问策略发挥的作用,演示如何显示和处理表单,涵盖添加到April 2011 Tools Update的新特性,如基架和HTML5项目模板,展示在客户端和服务器端验证模型的过程给出利用成员资格、援权和安全特性的忠告,讲解将TDD应用于ASP.NET MVC的方法。

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧