简体   繁体   中英

C# linq how to search data between two date

I am working on Windows base application I want search details between two date so am using linq query for that.

My query is:

mealsdbEntities objEntity = new mealsdbEntities();
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-CA");

DateTime OrderFromDate = DateTime.Parse(dtOrderFDate.Value.ToString("MM/dd/yyyy"));
DateTime OrderToDate = DateTime.Parse(dtOrderToDate.Value.ToString("MM/dd/yyyy"));
DateTime DelFromDate = DateTime.Parse(dtOrderDelFDate.Value.ToString("MM/dd/yyyy"));
DateTime DelToDate = DateTime.Parse(dtOrderDelTDate.Value.ToString("MM/dd/yyyy"));

var OrderList = (from ol in objEntity.orders
                 join cust_Con in objEntity.customermasters 
                      on ol.customer_id equals cust_Con.id
                 join cust_Cont in objEntity.customercontacts 
                      on ol.customer_id equals cust_Cont.customer_id
                 join oDish in objEntity.orderdishes 
                      on ol.id equals oDish.order_id
                 where cust_Con.company_name.Contains("" + txt_Cust_name.Text + "") 
                       && (EntityFunctions.TruncateTime(OrderFromDate) == OrderFromDate) 
                       //>= OrderFromDate) && (ol.createdon <= OrderToDate)) 
                       //&& (ol.deliverydate >= DelFromDate && ol.deliverydate <= DelToDate)
                 select new
                 {
                     ID = ol.id,
                     Title = ol.title,
                     Name = cust_Con.company_name,
                     Phone = cust_Cont.contact_phone,
                     // Item = disgcateg.dish_name,
                     DeliveryDate = ol.deliverydate,
                     DeliveryTime = ol.deliverytime,
                     Amount = ol.orderamount,
                     Status = ol.status
                 }).ToList();

but I am getting an error:

An unhandled exception of type 'System.Data.EntityCommandExecutionException' occurred in System.Data.Entity.dll

Additional information: An error occurred while executing the command definition. See the inner exception for details.

Can anyone kindly tell me where I am wrong?

You can use linq lambda

var startDate = new DateTime(2017, 1, 1);
var endDate = new DateTime(2017, 12, 31);

//this includes the last day
var endDatePlus = endDate.AddDays(1);

.Where(obj => obj.StartDate >= startDate && obj.EndDate < endDatePlus).ToList();

try this query

var OrderList = (from ol in objEntity.orders
                 join cust_Con in objEntity.customermasters 
                 on ol.customer_id equals cust_Con.id
                 join cust_Cont in objEntity.customercontacts 
                 on ol.customer_id equals cust_Cont.customer_id
                 join oDish in objEntity.orderdishes 
                 on ol.id equals oDish.order_id
                 where cust_Con.company_name.Contains("" + txt_Cust_name.Text + "") 
                 && (OrderFromDate.Date == OrderFromDate.Date) 
                 //>= OrderFromDate) && (ol.createdon <= OrderToDate)) 
                 //&& (ol.deliverydate >= DelFromDate && ol.deliverydate <= DelToDate)
                 select new
                 {
                     ID = ol.id,
                     Title = ol.title,
                     Name = cust_Con.company_name,
                     Phone = cust_Cont.contact_phone,
                     // Item = disgcateg.dish_name,
                     DeliveryDate = ol.deliverydate,
                     DeliveryTime = ol.deliverytime,
                     Amount = ol.orderamount,
                     Status = ol.status
                 }).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