小言_互联网的博客

linq 的join多表和select使用,let变量

396人阅读  评论(0)

join 分为inner join,left join ,cross join

select 分为select 和selectmany

通俗的来说 查询 有 平面查询和层次查询,关联有内关联和外关联

所以说根据项目中实际运用的情况,来选择是 inner join 还是 left join 而且是平面还是层次

inner join :

var  query = from a in tb1 join b in tb2  on a.id  equals  b.id select new { }

var  query = from a in tb1 join b in tb2  on  new{id=a.id}   equals  new{id=b.id}  select new { }
 

left join : 一定要DefaultIfEmpty()

var query = from s in db.tbStudents
                            join c in db.tbClass on s.ClassID equals c.ClassID into tbC
                            from tbCw in tbC.DefaultIfEmpty()
                            where s.ClassID == 3
                            select new
                            {
                                
                            };

 

分组: 一定要into

 var lineCount = from line in det.Org_DetectLine
                                    group line by line.StaID into g
                                    select new
                                    {
                                       StaID =  g.Key,
                                       LineCount = g.Count()
                                    };

 

有时候遇见主表从表都会有条件的情况,那么想一次查询出来的时候,直接在查询的过程中加入条件

var query =
                from c in dataContext.Main
                where c.Minor.Any (p => p.Price > 1000)
                select new
                {
                    c.Name,
                    Minor= from p in c.Minor  where p.Price > 1000
                                select new { p.Description, p.Price }
                };

这样可以满足,但是可以看到查的时候条件用了俩次,一次查,一次保存赋值,那么用let的话可以一次达到目的

var query =
                from c in dataContext.Main
                let Replace= from p in c.Minor
                                 where p.Price > 1000
                                 select new { p.Description, p.Price }
                where Replace.Any()
                select new { c.Name, Minor= Replace};

定义一个let变量来表示从表,这样select 去赋值的时候就是已经带有条件的从表数据了,省去了一次赋值

 

 

复合from子句实际上就是联接查询,大部分复合from子句(并不是所有,如cross join)的Linq完全可以用join子句的Linq来重写,两者生成的Sql也相同,推荐使用join Linq,这种写法与Sql更接近,更易读 

也就是说inner join,left join 使用from复合语句 或者是join 的写法都是没有啥区别的


var query1 = from o in dbContext.Orders
            from od in o.Order_Details
            select o;
 
var query2 = from o in dbContext.Orders
             join od in dbContext.Order_Details
             on o.OrderID equals od.OrderID

 

cross join:

如果from的对象均用表名,(from a in 表1  from b in 表2),则会转换成cross join

复合from语句其实就是 方法写法中的 selectmany 的用法

var query = db.Customers
              .Where(c => c.CustomerID == "ALFKI")
              .SelectMany(
                 c => db.Orders.Where(o => c.CustomerID == o.CustomerID),
                 (c, o) => new { c.ContactName, o.OrderDate }
                 );

 

 

selectmany 的用法还可以用来简便二重循环的情况


  //IEnumerable<Book>.Select 将序列中的Authors元素投影到新表中.
            IEnumerable<List<Author>> EnumerableOfListOfAuthors = Books.Select(book => book.Authors);
 
            foreach (var listOfAuthors in EnumerableOfListOfAuthors)
            {
                foreach (Author auth in listOfAuthors)
                {
                    Output.Items.Add(auth.Name); //添加到ListBox里面
                }
            }
 
            //IEnumerable<Book>.SelectMany  将序列的每个元素投影到 IEnumerable<T> 并将结果序列合并为一个序列。
            IEnumerable<Author> authors = Books.SelectMany(book => book.Authors);
            foreach (Author auth in authors)
            {
                Output2.Items.Add(auth.Name);
            }

一些列子:可以参考

https://www.cnblogs.com/ASPNET2008/archive/2008/12/19/1357690.html

https://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html

https://blog.csdn.net/simulationpd/article/details/83416869


转载:https://blog.csdn.net/Marzlam/article/details/101279795
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场