我正在使用Windows基础应用程序我希望在两个日期之间搜索详细信息 所以我正在使用linq查询。
我的查询是:
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();
但是我收到了一个错误:
未处理的类型&#39; System.Data.EntityCommandExecutionException&#39;发生在System.Data.Entity.dll
中附加信息:执行命令定义时发生错误。有关详细信息,请参阅内部异常。
有人可以告诉我哪里错了吗?
答案 0 :(得分:0)
您可以使用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();
答案 1 :(得分:0)
尝试此查询
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();