2012-07-08 66 views
0

我試着去篩選一個觀察的集合,像這樣過濾觀察到的集合

var IEquip = from eq in this.reportDocument.Document.InspectionData.Equipments where eq.PartData.ReportIncluded = true 
          orderby eq.PartData.Order ascending 
          select eq; 

這似乎是工作的查詢,但是當我嘗試迭代槽IEquip

foreach (EquipmentItem eq in IEquip) 
{ 
.... 
} 

所有ReportIncluded設置爲true,我可以看到ReportIncluded被調用的setter。我已經清空了循環內部的邏輯,結果相同。所有ReportInclude都是在循環的第一次迭代中設置的。我缺少什麼

回答

4

您正在使用賦值運算符,而不是平等的:

eq.PartData.ReportIncluded = true 
// instead of 
eq.PartData.ReportIncluded == true 
+0

Thx,在我的日子裏寫了很多VB6 – klashagelqvist 2012-07-09 13:23:42

3

不要混淆===。實際上,不需要將布爾值與truefalse文字進行比較(除非它是可空布爾值)。所以也許你可以只寫where eq.PartData.ReportIncluded沒有任何== true

+0

在其他情況下也會出現同樣的問題,就像人們說'if(someVar = false){...}',他們實際上分配給'someVar'。如果'someVar'是一個純布爾值,他們應該使用'if(!someVar)'代替。但是,如果'someVar'是一個可爲空的布爾值('bool?'),他們可以說'if(someVar == false)'。 **的右邊**將被自動「提升」爲「(bool?)false」。好消息是'someVar =(bool?)false'的返回類型與'someVar ==(bool?)false'的返回類型不同,所以如果不小心使用'='而不是' =='可空。 – 2012-07-08 20:18:41