ef6 常用方法

#ef6 左连接写法--from baidu

 

using (var context = new DbContext())
{
	var query = from t1 in context.LeftTable
	join t2 in context.RightTable on t1.Id equals t2.LeftId into temp
	from t2 in temp.DefaultIfEmpty()
	select new
	{
		LeftItemId = t1.Id,
		LeftItemName = t1.Name,
		RightItemId = t2 != null ? t2.Id : (int?)null,
		RightItemName = t2 != null ? t2.Name : (string)null
	};
	 
	var results = query.ToList();
}

  

 
#分组 合计
 
 
#
var invoices2 = await _dbContext.Beneficiaries
.Where(dbEntry => dbEntry.Id == beneficiaryId && dbEntry.ProviderId == providerId)
.SelectMany(dbEntry => dbEntry.Invoices
.GroupBy(dbEntry => dbEntry.IssueDate.Month)
.Select(dbEntry => new
{
	IssueMonth = dbEntry.Key,
	VAT = dbEntry.Sum(invoice => invoice.VAT),
	TotalPay = dbEntry.Sum(invoice => invoice.InvoiceEntries.Sum(entry => entry.DelegateHourlyRate)),
	TotalSell = dbEntry.Sum(invoice => invoice.InvoiceEntries.Sum(entry => entry.BeneficiaryHourlyRate))
}))
.Where(group => group.IssueMonth <= _todayDate.UtcNow.Month && group.IssueMonth >= _todayDate.UtcNow.Month - (int)by)
.ToListAsync();

  

 

posted @ 2024-12-13 09:25  以函  阅读(1)  评论(0编辑  收藏  举报