我习惯在SQL Server中工作,在Linq中转换它有问题。 它是 Group in Join in Linq 。
SQL查询:
Select
Convert(varchar,CreateOn,101),
count(*),
Note as Total
from TrnExpense
group by Convert(varchar,CreateOn,101),Note
order by Convert(varchar,CreateOn,101) desc
SQL服务器中的我的输出如下:
linq的输出也被接受。任何帮助将不胜感激。
为TrnExpense生成的类如下:
public partial class TrnExpense
{
public int id { get; set; }
public Nullable<int> TripId { get; set; }
public Nullable<int> TruckId { get; set; }
public Nullable<int> TrailerId { get; set; }
public Nullable<int> DriverId { get; set; }
public Nullable<int> ReferType { get; set; }
public Nullable<int> VendorId { get; set; }
public Nullable<int> ExpenseType { get; set; }
public Nullable<int> CountryId { get; set; }
public Nullable<int> StateId { get; set; }
public Nullable<int> CityId { get; set; }
public Nullable<double> Qty { get; set; }
public Nullable<int> qbStatus { get; set; }
public Nullable<int> qbTaxCode { get; set; }
public string QtyUnit { get; set; }
public Nullable<decimal> Rate { get; set; }
public Nullable<decimal> PreTaxAmt { get; set; }
public Nullable<decimal> Outstanding { get; set; }
public string InvoiceNo { get; set; }
public Nullable<double> ServiceFee { get; set; }
public Nullable<decimal> HstTax { get; set; }
public Nullable<double> TaxOne { get; set; }
public Nullable<double> TaxTwo { get; set; }
public string Currency { get; set; }
public string CardNo { get; set; }
public string UnitNo { get; set; }
public Nullable<int> PaymentId { get; set; }
public Nullable<int> SettlementId { get; set; }
public Nullable<int> VendorPayId { get; set; }
public string Note { get; set; }
public Nullable<System.DateTime> ExpenseDate { get; set; }
public Nullable<System.DateTime> DueDate { get; set; }
public string OwnerDeductionYN { get; set; }
public Nullable<int> Status { get; set; }
public Nullable<int> PayStatus { get; set; }
public Nullable<int> DriverDedStatus { get; set; }
public Nullable<int> DriverSettleId { get; set; }
public Nullable<int> UpdateBy { get; set; }
public Nullable<System.DateTime> LastUpdate { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public Nullable<int> CompanyId { get; set; }
public Nullable<decimal> AmountPaid { get; set; }
public Nullable<int> ExpPaymentType { get; set; }
public Nullable<bool> IsExcludeTax { get; set; }
public Nullable<double> Discount { get; set; }
public Nullable<System.DateTime> TransferToQBDate { get; set; }
public string CurrencyCode { get; set; }
public Nullable<int> DocumentUploadId { get; set; }
public string DeductionTypeFlag { get; set; }
public Nullable<decimal> NetAmount { get; set; }
public bool IsActive { get; set; }
public Nullable<System.DateTime> CreateOn { get; set; }
public string CreateBy { get; set; }
public Nullable<System.DateTime> ModifyOn { get; set; }
public string ModifyBy { get; set; }
public Nullable<decimal> Rate1Per { get; set; }
public Nullable<decimal> Rate2Per { get; set; }
public Nullable<bool> TaxIncluded { get; set; }
public Nullable<bool> TaxOnly { get; set; }
public Nullable<bool> IncludeTax1 { get; set; }
public Nullable<bool> IncludeTax2 { get; set; }
public string Source { get; set; }
public Nullable<int> SourceId { get; set; }
public string QbId { get; set; }
public Nullable<System.DateTime> QbLastSyncDate { get; set; }
public virtual CityState CityState { get; set; }
public virtual Country Country { get; set; }
public virtual Driver Driver { get; set; }
public virtual ExpenseType ExpenseType1 { get; set; }
public virtual StateCountry StateCountry { get; set; }
public virtual Vehicle Vehicle { get; set; }
public virtual Vehicle Vehicle1 { get; set; }
public virtual Vendor Vendor { get; set; }
}
答案 0 :(得分:2)
{{1}}
答案 1 :(得分:0)
请尝试以下代码,用lambda表达式编写:
var resultSet=TrnExpenses.GroupBy(s=>new
{
s.CreateOn.Value.Date, s.Note
})
.OrderByDescending(s=>s.Key.Date)
.Select(s=>new
{
s.Key.Date,
s.Key.Note,
Count = s.Count()
});
答案 2 :(得分:0)
我刚试过,发现了以下内容,只想添加:
要使用日期,请使用日期时间 Column.Value.Date 。
以下代码将清除相同的内容。
(from t in TrnExpenses
group t by new { t.CreateOn.Value.Date, t.Note } into g
orderby g.Key.Date descending
select new{
CreatedOn = g.Key.Date,
Note = g.Key.Note,
Count = g.Count()
}).ToList()