我在嘗試使用SkipWhile
過濾對象,但它不評估多個條件。如何在多個條件下使用SkipWhile
下面是一個示例代碼演示問題,
int[] numbers = { 1, 2, 3, 4, 5 };
var result = numbers.SkipWhile(n => n < 2 && n != 2).ToList();
此查詢選擇2,3,4,5
,其省略了第二個條件(n != 2
)中,當所述第一條件是true
。
是否有可能使查詢評估兩個條件?
編輯:
我的實際情況是像
... dateRanges
.OrderBy(d=>d.Sequence)
.SkipWhile(d => d.FromDate <= currentDate && d.ToDate >= currentDate)
.Skip(1).First();
這是經營上的日期時間申請,在列表中選擇下一個對象
編輯2:
我創建了一個示例程序,這是一個SIM卡ILAR到我的實際代碼
類保存數據,
public class DateRange
{
public int Sequence { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
}
計劃
static void Main(string[] args)
{
var dateRanges = new List<DateRange>(){
new DateRange{Sequence = 1 , FromDate = new DateTime(2014,1,1), ToDate = new DateTime(2014,1,31)},
new DateRange{Sequence = 2 , FromDate = new DateTime(2014,2,1), ToDate = new DateTime(2014,2,28)},
new DateRange{Sequence = 3 , FromDate = new DateTime(2014,3,1), ToDate = new DateTime(2014,3,31)},
new DateRange{Sequence = 4 , FromDate = new DateTime(2014,4,1), ToDate = new DateTime(2014,4,30)},
new DateRange{Sequence = 5 , FromDate = new DateTime(2014,5,1), ToDate = new DateTime(2014,5,31)},
};
var myDate = new DateTime(2014, 2, 10); // A Date in Fabruary
//This query selects {2, 2014/2/1, 2014/2/28}
var selectedItem = dateRanges.OrderBy(d => d.Sequence)
.Where(d => d.FromDate <= myDate && d.ToDate >= myDate)
.First();
//What I actually need to select is {3, 2014/3/1, 2014/3/31}
//Which is next item of the list
//This is what i have tried
//But this query also selects {2, 2014/2/1, 2014/2/28}
var nextItem = dateRanges.OrderBy(d => d.Sequence)
.SkipWhile(d => d.FromDate <= myDate && d.ToDate >= myDate)
.Skip(1).First();
//Because, results of this part of query returns objects from {1, 2014/1/1, 2014/1/31} ...
var unexpectdItems = dateRanges.OrderBy(d => d.Sequence)
.SkipWhile(d => d.FromDate <= myDate && d.ToDate >= myDate);
}
請檢查更新,我的實際需求是選擇當這兩個條件滿足下一個對象... – Nalaka526
@ Nalaka526:嗯沒有關於您的數據的任何信息和你得到的結果,我們不能真正幫助你。我建議你編輯你的問題,以包括一個簡短的*完整的例子,展示這個問題。但我非常懷疑問題在於執行'SkipWhile'。 –
我已經用示例代碼更新了問題,請檢查... – Nalaka526