简体   繁体   中英

What would be the linq for this SQL Group Join

I use to work in SQL Server ,Having issue in converting same in Linq . Its a Group Join in Linq .

SQL Query :

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

My Output in SQL server follows :

在此处输入图片说明

Output in linq is also accepted same .Any help will be appreciated .

Class Generated for TrnExpense follows :

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; }
    }
var result = (from t in TrnExpense 
group t by new { t.CreatedOn.Date, t.Note } into g
orderby g.Key.Date descending
select new{
    CreatedOn = g.Key.Date,
    Note = g.Key.Note,
    Count = g.Count()
}).ToList();

Please try following code ,Written in lambda expression:

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()
});

I just tried and found the following and simply want to add on :

To access the date From datetime Column.Value.Date is used .

Following code will clear the same .

(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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM