Linq 和 lambd

这里说的不是二者的区别,而是二者的联合应用,因为好多函数在sql拼接的时候不支持,所以检索数据需要联合使用。

今天不去详细的阐述理论,只是放置一个我项目中的实例,以备自己以后查阅,也给有需要的朋友一个参考机会。

如下代码可靠可行:

 var datable = db.SalerTransaction.WhereIf(o => o.saler_username == ventity.saler_username.Trim(), !string.IsNullOrWhiteSpace(ventity.saler_username))
                    .WhereIf(o => o.cus_username == ventity.cus_username.Trim(), !string.IsNullOrWhiteSpace(ventity.cus_username))
                    .WhereIf(o => o.saler_type == ventity.saler_type, !string.IsNullOrWhiteSpace(ventity.saler_type))
                    .WhereIf(o => o.cur_versionno == ventity.cur_versionno, !string.IsNullOrWhiteSpace(ventity.cur_versionno))
                    .WhereIf(o => o.to_versionno == ventity.to_versionno, !string.IsNullOrWhiteSpace(ventity.to_versionno))
                    .WhereIf(o => o.created > ventity.startTime.Value, ventity.startTime != null)
                    .WhereIf(o => o.created < ventity.endTime.Value, ventity.endTime != null)
                    .WhereIf(o => o.type == ventity.type, !string.IsNullOrWhiteSpace(ventity.type));

                IQueryable<int> tempable = null;
                if (ventity.paymentstate == 2) {//钱全部结清的记录
                    tempable = from a in datable
                                   join b in db.SalerTransactionLog.Where(o => o.state == 2) on a.id equals b.st_id 
                                   group new { a, b } by new { a.id, a.init_price, a.discount_price } into x
                                   where x.Sum(o => o.b.money) >= x.Key.init_price-x.Key.discount_price
                                   select x.Key.id;         
                } else if (ventity.paymentstate == 3) {//定金且钱未补够的记录
                    tempable = from a in datable
                                   join b in db.SalerTransactionLog.Where(o => o.state == 2) on a.id equals b.st_id
                                   group new { a, b } by new { a.id, a.init_price,a.discount_price } into x
                                   where x.Sum(o => o.b.money) < x.Key.init_price-x.Key.discount_price
                                   select x.Key.id;
                }
                if (tempable != null) {
                    page.record_count = tempable.Count();
                    if (page.record_count == 0) {
                        return new ServiceResult(new List<VSalerTransaction>(), 0);
                    }
                    datable = datable.Where(o => tempable.Contains(o.id));
                } else page.record_count = datable.Count();

                datable = datable.OrderByDescending(o => o.id).Skip(page.page_size * (page.page_index - 1)).Take(page.page_size);

后来我发现这样做有点绕弯弯,所以做了修改

 var datable = db.SalerTransaction.WhereIf(o => o.saler_username == ventity.saler_username.Trim(), !string.IsNullOrWhiteSpace(ventity.saler_username))
                    .WhereIf(o => o.cus_username == ventity.cus_username.Trim(), !string.IsNullOrWhiteSpace(ventity.cus_username))
                    .WhereIf(o => o.saler_type == ventity.saler_type, !string.IsNullOrWhiteSpace(ventity.saler_type))
                    .WhereIf(o => o.cur_versionno == ventity.cur_versionno, !string.IsNullOrWhiteSpace(ventity.cur_versionno))
                    .WhereIf(o => o.to_versionno == ventity.to_versionno, !string.IsNullOrWhiteSpace(ventity.to_versionno))
                    .WhereIf(o => o.created > ventity.startTime.Value, ventity.startTime != null)
                    .WhereIf(o => o.created < ventity.endTime.Value, ventity.endTime != null)
                    .WhereIf(o => o.type == ventity.type, !string.IsNullOrWhiteSpace(ventity.type));

                if (ventity.paymentstate == 2) {//全部结清的记录
                    datable = from a in datable
                                   join b in db.SalerTransactionLog.Where(o => o.state == 2) on a.id equals b.st_id 
                                   group new { a, b } by new { a } into x
                                   where x.Sum(o => o.b.money) >= x.Key.a.init_price-x.Key.a.discount_price
                                   select x.Key.a;         
                } else if (ventity.paymentstate == 3) {//定金且钱未补够的记录
                    datable = from a in datable
                                   join b in db.SalerTransactionLog.Where(o => o.state == 2) on a.id equals b.st_id
                                   group new { a, b } by new { a } into x
                                   where x.Sum(o => o.b.money) < x.Key.a.init_price-x.Key.a.discount_price
                                   select x.Key.a;
                }
                page.record_count = datable.Count();
                datable = datable.OrderByDescending(o => o.id).Skip(page.page_size * (page.page_index - 1)).Take(page.page_size);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑羽酣天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值