Linq to entity多表查询如何返回查询结果
		
		1、定义一个新的类别,类别包含了多个表的属性
- public class AB
 - {
 - public int a_id;
 - public string a_name;
 - public int b_id;
 - public string b_name;
 - }
 
2、使用视图,和上面基本一样
其实就是使用SQL语句
示例:
select a.id,a.name,b.id ,b.name from a join b on a.id=b.id
3、使用储存过程,直接返回结果集
同上。
4、使用匿名对像,返回IQueryable, 例:
- public IQueryable GetAllList()
 - {
 - var query = from a in tb_a join b in tb_b on a.id equels b.id select new {a.id,b.id};
 - return query;
 - }
 
5、使用LINQ连接查询
- List<tb_a> aList=aBLL.GetList();
 - List<tb_b> bList=bBLL.GetList();
 - var query = from a in aListjoin b in bListon a.id equels b.id select new {a.id,b.id};
 - dgvList.Datasource=query.ToList();
 
6、使用循环
- List<tb_a> aList=aBLL.GetList();
 - List<tb_b> bList=bBLL.GetList();
 - eg:绑定DataGridView
 - foreach(tb_a a in aList)
 - {
 - Object[] row={a.id,b.id,aList.FirstOrDefault(f=>f.b_id=a.id).name}
 - DataGridView1.Rows.Add(row);
 - }
 
7.使用System.Func委托 (参考:Returning var from a method in C# 3.0)
数据层:
- public IEnumerable<T> GetQuery<T>(Func<DB.Repair, DB.Operator, T> func)
 - {
 - var query = from r in db.Repair
 - join o in db.Operator on r.fchrOperatorID equals o.fchrOperatorID
 - orderby r.fchrReceiveDate descending
 - select func(r, o);
 - return query;
 - }
 
业务逻辑层:
- var query = repairBLL.GetQuery((repair, oper) => new
 - {
 - repair.fchrRepairID,
 - repair.fchrRepairCode,
 - oper.fchrName,
 - repair.fchrReceiveDate
 - }
 
这样返回的结果在业务逻辑层里仍然是真正的匿名类型,可以直接使用了。
---------------------------------------------------------------------------------------------
不断更新中,如果大家有什么好的方法,在留言里写下,我会定期加上来!谢谢!
^_^
								
				
				============ 欢迎各位老板打赏~ ===========
			
		与本文相关的文章
- · Linq to Entity 连接(join) Linq to Object
 - · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
 - · .NET8实时更新nginx ip地址归属地
 - · 解决.NET Blazor子组件不刷新问题
 - · .NET8如何在普通类库中引用 Microsoft.AspNetCore
 - · .NET8 Mysql SSL error
 - · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
 - · .NETCORE 依赖注入服务生命周期
 - · asp.net zero改mysql
 - · .NET5面试汇总
 - · .Net连接Mysql数据库的Convert Zero Datetime日期问题
 - · vue使用element-ui中的Message 、MessageBox 、Notification
 

灰常感谢!!
4、使用匿名对像,返回IQueryable,
,返回的两个列,为什么会错呀
什么错?
是我写错了,不好意思,是对的,谢谢。
不谢,互相交流