C# Linq查询的一个问题
我想用linq 分组,查询但是查询条件的参数是动态传进来的..有时候我需要查询全部的.有没有什么关键字什么的能全部查询(本来不写查询条件也是查全部)
代码如下..其中的hour1和hour2,"2013/3/17","2013/3/19"都是要传进来的有时要查询全部又不需要.有没有什么好办法
int hour1 = 17;
int hour2 = 18;
var result = from item in AdvertisementFocusDataSource.ls_advertisementFocusInfo
where item.FocusTime.Date.CompareTo(DateTime.Parse("2013/3/17")) >= 0 && item.FocusTime.Date.CompareTo(DateTime.Parse("2013/3/19")) <= 0
&& item.FocusTime.Hour >=hour1 &&item.FocusTime.Hour<=hour2
group item by item.ContentName into g
select new
{
Mkey= g.Key,
TouchNum = g.Sum(p=>p.TouchNum),
FocusNum=g.Sum(p=>p.FocusNum),
BookingNum=g.Sum(p=>p.BookingNum),
PurchaseNum=g.Sum(p=>p.PurchaseNum),
};
------解决方案-------------------- bool bHasCondition=false; //是否有条件,改变此值来决定查询是否有条件
var result = from item in AdvertisementFocusDataSource.ls_advertisementFocusInfo
where !bHasCondition ? 1 == 1 : (item.FocusTime.Date.CompareTo(DateTime.Parse("2013/3/17")) >= 0 && item.FocusTime.Date.CompareTo(DateTime.Parse("2013/3/19")) <= 0
&& item.FocusTime.Hour >= hour1 && item.FocusTime.Hour <= hour2)