LINQ - Where 条件 - "if-else" 个案例
LINQ - Where Conditions - "if-else" Cases
我有以下 Linq 查询。我想要的是如果我确实搜索特定的 year
,我应该得到那个 year
的相应记录。否则只显示当年的记录。同样的方法 searchtext
也必须完成。
public ActionResult Search(string searchtext, int? year)
{
string selyear = year.ToString();
string curyear = (DateTime.Now.Year).ToString();
hdms = from t in db.HOLIDAY
where
(year == null || t.DOH.StartsWith(selyear)) &&
(searchtext == "" || t.HOLIDAY_NAME == searchtext)
select new HOLIDAYDETAILS
{
DOH = t.DOH,
};
....
}
我也想满足这个 if (year == null) then t.DOH.StartsWith(curyear)
和
if (searchtext == "") then t.HOLIDAY_NAME != "Sunday" && t.HOLIDAY_NAME != "Saturday"
.
另外如果两者同时为null,则两个条件都要同时满足
如何在上面的 where 子句中也给出这个?
如果检查年份是否为空是你的问题,那么你可以这样做-
public ActionResult Search(int? year)
{
string selyear = year.ToString();
string queryyear = (string.IsNullOrEmpty(selyear)) // checking for null
? (DateTime.Now.Year).ToString() // current year
: selyear; // year in query
hdms = from t in db.HOLIDAY
join t1 in db.EMPLOYEE on t.UPDATED_BY equals t1.EMP_CODE
where
t.DOH.StartsWith(queryyear) // comparing queryyear
orderby
t.DOH
select new HOLIDAYDETAILS
{
DOH = t.DOH,
};
....
}
希望这对你有用...
public ActionResult Search(int? year)
{
string curyear = (DateTime.Now.Year).ToString();
string selyear = year!=null? year.ToString() : curyear;
var query = from t in db.HOLIDAY
join t1 in db.EMPLOYEE on t.UPDATED_BY equals t1.EMP_CODE
select new {t,t1};
query = query.Where(o=>o.t.DOH.StartsWith(selyear));
if (searchtext == "")
{
query = query.Where(o=>o.t.HOLIDAY_NAME != "Sunday" && o.t.HOLIDAY_NAME != "Saturday");
}
hdms = query.OrderBy(o=>o.t.DOH).Select(o=> new HOLIDAYDETAILS
{
DOH = o.t.DOH,
});
....
}
我有以下 Linq 查询。我想要的是如果我确实搜索特定的 year
,我应该得到那个 year
的相应记录。否则只显示当年的记录。同样的方法 searchtext
也必须完成。
public ActionResult Search(string searchtext, int? year)
{
string selyear = year.ToString();
string curyear = (DateTime.Now.Year).ToString();
hdms = from t in db.HOLIDAY
where
(year == null || t.DOH.StartsWith(selyear)) &&
(searchtext == "" || t.HOLIDAY_NAME == searchtext)
select new HOLIDAYDETAILS
{
DOH = t.DOH,
};
....
}
我也想满足这个 if (year == null) then t.DOH.StartsWith(curyear)
和
if (searchtext == "") then t.HOLIDAY_NAME != "Sunday" && t.HOLIDAY_NAME != "Saturday"
.
另外如果两者同时为null,则两个条件都要同时满足
如何在上面的 where 子句中也给出这个?
如果检查年份是否为空是你的问题,那么你可以这样做-
public ActionResult Search(int? year)
{
string selyear = year.ToString();
string queryyear = (string.IsNullOrEmpty(selyear)) // checking for null
? (DateTime.Now.Year).ToString() // current year
: selyear; // year in query
hdms = from t in db.HOLIDAY
join t1 in db.EMPLOYEE on t.UPDATED_BY equals t1.EMP_CODE
where
t.DOH.StartsWith(queryyear) // comparing queryyear
orderby
t.DOH
select new HOLIDAYDETAILS
{
DOH = t.DOH,
};
....
}
希望这对你有用...
public ActionResult Search(int? year)
{
string curyear = (DateTime.Now.Year).ToString();
string selyear = year!=null? year.ToString() : curyear;
var query = from t in db.HOLIDAY
join t1 in db.EMPLOYEE on t.UPDATED_BY equals t1.EMP_CODE
select new {t,t1};
query = query.Where(o=>o.t.DOH.StartsWith(selyear));
if (searchtext == "")
{
query = query.Where(o=>o.t.HOLIDAY_NAME != "Sunday" && o.t.HOLIDAY_NAME != "Saturday");
}
hdms = query.OrderBy(o=>o.t.DOH).Select(o=> new HOLIDAYDETAILS
{
DOH = o.t.DOH,
});
....
}