在使用Linq to Sql的时候,进行两个表的左连接的时候要注意defaultifempty的使用,这个函数本来的意思即是:如果为空则使用默认值代替,默认值为 NULL ,当然也可以使用defaultifempty的另一个重载指定默认。如要了解该函数的详细使用,请看文档:http://msdn.microsoft.com/zh-cn/library/system.linq.enumerable.defaultifempty.aspx

      看下面的例子:

            var q = (from c in
                         (from a1 in db.StoreIns 
                          group a1 by a1.StoreNum into g 
                          select new { storenum = g.Select(p => p.StoreNum).FirstOrDefault(), total = g.Sum(p =>                                        p.Quantity) })
                     join d in
                         (from a2 in db.StoreOuts
                          group a2 by a2.StoreNum into g2
                          select new { storeNum = g2.Select(p => p.StoreNum).FirstOrDefault(), totalout = g2.Sum(p                          => p.Quantity) }) on c.storenum
                     equals d.storeNum into ord 
                     from t in ord.DefaultIfEmpty()
                     select new 
                     {
                         storeNum = c.storenum,
                         actalIn=c.total,
                         actualOut= (t.totalout==null) ? 0 :t.totalout,
                         actualNum = c.total - ((t.totalout==null) ? 0 :t.totalout)
                     }).ToList();

 

             分解解释:

             (1)

                   from a1 in db.StoreIns 
                          group a1 by a1.StoreNum into g 
                          select new { storenum = g.Select(p => p.StoreNum).FirstOrDefault(), total = g.Sum(p =>                                        p.Quantity) }

 

                  表示在入库表中按照库的编号StorNum进行分组统计每个库的实际总量,通过匿名类设置了两个属性:storenum和total。需要特别注意的是按照分组统计之后,会形成storenum和total的关系是 : 多对一的关系,并且这些storenum是相同的值, 为了后面要进行的左连接,需要取出storenum,此时只能调用FirstOrDefault()函数而不能调用Fisrt(),因为Fisrt()取得是storenum对象,这个对象了存储的是多个相同的值,在后面得左连接就无法进行。

            (2)

                from a2 in db.StoreOuts
                          group a2 by a2.StoreNum into g2
                          select new { storeNum = g2.Select(p => p.StoreNum).FirstOrDefault(), totalout = g2.Sum(p                          => p.Quantity) }

 

          表示在出库表中按照库编号进行分组统计每个库的实际总量。

 

             (3)

                下面要对上述两步查询的到数据进行左连接。

               from c in
                         (from a1 in db.StoreIns 
                          group a1 by a1.StoreNum into g 
                          select new { storenum = g.Select(p => p.StoreNum).FirstOrDefault(), total = g.Sum(p =>                                        p.Quantity) })
                     join d in
                         (from a2 in db.StoreOuts
                          group a2 by a2.StoreNum into g2
                          select new { storeNum = g2.Select(p => p.StoreNum).FirstOrDefault(), totalout = g2.Sum(p                          => p.Quantity) }) on c.storenum
                     equals d.storeNum into ord 
                     from t in ord.DefaultIfEmpty()
                     select new 
                     {
                         storeNum = c.storenum,
                         actalIn=c.total,
                         actualOut= (t.totalout==null) ? 0 :t.totalout,
                         actualNum = c.total - ((t.totalout==null) ? 0 :t.totalout)
                     }

                在上面要注意的是d的访问范围,当……into ord……后,只能使用t来访问数据。如下的访问方式就是错误的:actual=c.total-((d.totalout==null)?0:d.totalout) 就会提示上下文不存在d的错误提示。

                这句表示的是使用(1)中数据左连接(2)中的数据。如果(2)中的相关项不存在则使用默认值NULL代替。