public List<HD_RECORD_BEFORE> checkbedno(string devicebarcode01, string devicebarcode02)
{
var query = (from x in _repo.HD_RECORD_BEFORE
join y in _repo.HD_DEVICED_HISTORY_LIST
on devicebarcode01 equals y.DEV_BARCODE
where y.END_RECORD_DATETIME == null &&
x.PAT_SCHEDULE_GUID == y.PAT_SCHEDULE_GUID &&
DbFunctions.TruncateTime(x.DIA_DATE) == DbFunctions.TruncateTime(y.START_RECORD_DATETIME)
select x
).ToList<HD_RECORD_BEFORE>();
return query;
}
這是我的一段LINQ查詢,目前我想多做一個判斷
就是參數devicebarcode01 不為空值或null做A查詢,
devicebarcode02不為空值或null做B查詢,
想請問這個有辦法實現嗎?我目前暫時寫不出,希望能給個參考
謝謝。
看過你的回覆 你應該可以改成這樣
利用三元運算子來取得這次要使用的條件
public List<HD_RECORD_BEFORE> checkbedno(string devicebarcode01, string devicebarcode02)
{
string queryCondition = !string.IsNullOrEmpty(devicebarcode01) ?
devicebarcode01 : devicebarcode02;
var query = (from x in _repo.HD_RECORD_BEFORE
join y in _repo.HD_DEVICED_HISTORY_LIST
on queryCondition equals y.DEV_BARCODE
where y.END_RECORD_DATETIME == null &&
x.PAT_SCHEDULE_GUID == y.PAT_SCHEDULE_GUID &&
DbFunctions.TruncateTime(x.DIA_DATE) == DbFunctions.TruncateTime(y.START_RECORD_DATETIME)
select x
).ToList<HD_RECORD_BEFORE>();
return query;
}
我也是這樣想...
我原先是這樣想的
if (!string.IsNullOrEmpty(devicebarcode01))
{
var query = (from x in _repo.HD_RECORD_BEFORE
join y in _repo.HD_DEVICED_HISTORY_LIST
on devicebarcode01 equals y.DEV_BARCODE
where y.END_RECORD_DATETIME == null &&
x.PAT_SCHEDULE_GUID == y.PAT_SCHEDULE_GUID &&
DbFunctions.TruncateTime(x.DIA_DATE) == DbFunctions.TruncateTime(y.START_RECORD_DATETIME)
select x
).ToList<HD_RECORD_BEFORE>();
}
return query;
不過這樣是錯誤的
你JOIN語法打錯了吧
應該是 on x.a欄位 equals y.a欄位
where devicebarcode01 == ...
跟SQL語法一樣啊
var query = (from x in _repo.HD_RECORD_BEFORE
join y in _repo.HD_DEVICED_HISTORY_LIST
on devicebarcode01 equals y.DEV_BARCODE
where y.END_RECORD_DATETIME == null &&
x.PAT_SCHEDULE_GUID == y.PAT_SCHEDULE_GUID &&
DbFunctions.TruncateTime(x.DIA_DATE) ==
DbFunctions.TruncateTime(y.START_RECORD_DATETIME)
select x
).ToList<HD_RECORD_BEFORE>();
return query;