C# EF+LINQ 查询主表明细表数据查询条件写法
案例1:只返回主表数据
C# 全选
//主从表的统一查询内容
if (!input.content.IsEmpty())
{
//查询明细(子查询)
var q_Sub = q_APs.Where(w => false
|| w.InvoiceNo == input.content
|| w.InvoiceName == input.content
|| w.OrderNo == input.content
|| (w.Remark ?? "").Contains(input.content));
//主表
q_AP = q_AP.Where(w => (false
|| w.APNO == input.content
|| w.SupplierCode == input.content
|| (w.SupplierName ?? "").Contains(input.content)) || q_Sub.Any(x => x.APNO == w.APNO));
}
return q_AP.ToList();
案例2:返回主从表关联数据
C# 全选
public List<res_tb_SS> Query(req_SS input)
{
var q_SS = _Database.GetQueryable<tb_SS>(); //主表
var q_SSs = _Database.GetQueryable<tb_SSs>(); //从表
//明细表单独的过滤器
var q_Filter = _Database.GetQueryable<tb_SSs>();
if (!input.DZNO.IsEmpty())
q_SS = q_SS.Where(w => w.DZNO == input.DZNO);
if (input.DocDateFrom > Globals.MinSqlDate)//1900-01-01
{
q_SS = q_SS.Where(w => w.DocDate >= input.DocDateFrom);
}
if (input.DocDateTo > Globals.MinSqlDate)
{
q_SS = q_SS.Where(w => w.DocDate <= input.DocDateTo);
}
if (!input.DocType.IsEmpty())
q_SS = q_SS.Where(w => w.DocType == input.DocType);
if (!input.PaymentType.IsEmpty())
q_SS = q_SS.Where(w => w.PaymentType == input.PaymentType);
if (!input.SupplierCode.IsEmpty())
q_SS = q_SS.Where(w => w.SupplierCode == input.SupplierCode);
// 模糊匹配 content
if (!input.content.IsEmpty())
{
// 先筛选从表
q_Filter = q_SSs.Where(w =>
w.DocNo == input.content ||
w.ProductCode == input.content ||
w.CustomerOrderNo == input.content ||
(w.DocRemark != null && w.DocRemark.Contains(input.content)) ||
(w.ProductName != null && w.ProductName.Contains(input.content)) ||
(w.Model != null && w.Model.Contains(input.content)) ||
(w.Remark != null && w.Remark.Contains(input.content)));
// 主表条件 + 关联从表条件
q_SS = q_SS.Where(w =>
w.DZNO == input.content ||
w.SupplierCode == input.content ||
w.SupplierContact == input.content ||
w.SupplierContactTel == input.content ||
(w.SupplierName != null && w.SupplierName.Contains(input.content)) ||
(w.Remark != null && w.Remark.Contains(input.content)) ||
q_Filter.Any(x => x.DZNO == w.DZNO)); // 这里使用已筛选的 q_SSs
}
var resList = from a in q_SS
//关键: 如果 input.content 为空,则不过滤从表
join b in q_SSs.Where(w => input.content.IsEmpty() || q_Filter.Any(x => x.isid == w.isid))
on a.DZNO equals b.DZNO into tmp
from b in tmp.DefaultIfEmpty()
select new res_tb_SS
{
//主表
isid = a.isid,
DZNO = a.DZNO,
DocDate = a.DocDate,
SupplierCode = a.SupplierCode,
SupplierName = a.SupplierName,
SupplierContact = a.SupplierContact,
SupplierContactTel = a.SupplierContactTel,
PaymentType = a.PaymentType,
TotalAmount = a.TotalAmount,
TotalAmountExclTax = a.TotalAmountExclTax,
TotalTaxAmount = a.TotalTaxAmount,
TotalSettleAmount = a.TotalSettleAmount,
TotalSettleAmountBal = a.TotalSettleAmountBal,
Remark = a.Remark,
Year = a.Year,
Month = a.Month,
FlagPeriod = a.FlagPeriod,
PeriodId = a.PeriodId,
FlagApp = a.FlagApp,
AppUser = a.AppUser,
AppDate = a.AppDate,
CreationDate = a.CreationDate,
CreatedBy = a.CreatedBy,
LastUpdateDate = a.LastUpdateDate,
LastUpdatedBy = a.LastUpdatedBy,
//明细表
isid_detail = b == null ? null : b.isid,
Queue = b == null ? null : b.Queue,
OrderId = b == null ? null : b.OrderId,
DocNo = b == null ? null : b.DocNo,
DocDetail_isid = b == null ? null : b.DocDetail_isid,
DocOrderId = b == null ? null : b.DocOrderId,
DocType = b == null ? null : b.DocType,
DocDate_detail = b == null ? null : b.DocDate,
DocRemark = b == null ? null : b.DocRemark,
PONO = b == null ? null : b.PONO,
ARAP = b == null ? null : b.ARAP,
LocationID = b == null ? null : b.LocationID,
ProductCode = b == null ? null : b.ProductCode,
ProductName = b == null ? null : b.ProductName,
ProductType = b == null ? null : b.ProductType,
Model = b == null ? null : b.Model,
CustomerOrderNo = b == null ? null : b.CustomerOrderNo,
Unit = b == null ? null : b.Unit,
OrderQty = b == null ? null : b.OrderQty,
Quantity = b == null ? null : b.Quantity,
Price = b == null ? null : b.Price,
Amount = b == null ? null : b.Amount,
PriceExclTax = b == null ? null : b.PriceExclTax,
AmountExclTax = b == null ? null : b.AmountExclTax,
TaxRate = b == null ? null : b.TaxRate,
TaxAmount = b == null ? null : b.TaxAmount,
Currency = b == null ? null : b.Currency,
CurrencyRate = b == null ? null : b.CurrencyRate,
SettleAmount = b == null ? null : b.SettleAmount,
SettleAmountBal = b == null ? null : b.SettleAmountBal,
};
var resultList = new List<res_tb_SS>();
if (input.top > 0)
resultList = resList.Distinct().Take(input.top).OrderByDescending(o => o.CreationDate).ToList();
else
resultList = resList.Distinct().OrderByDescending(o => o.CreationDate).ToList();
//返回查询结果
return resultList.ToList();
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网