目標是讓一些代碼爲Where
調用建立過濾條件。建設條件稍後進行過濾
在FoxPro中,我會使用構建爲字符串的條件("foo == 3 && bar > 5"
)並稍後執行它。
我想在C#中類似的方法 - 創建字符串,並把它作爲在下面的代碼條件,但找不到辦法做到這一點:
string Condition = "";
...
if (xyz > 0)
Condition = "scr.ZipCode = 12345";
if (xyz > 1)
Condition = "scr.ZipCode = 23456";
if (xyz > 2)
Condition = "scr.ZipCode = 34567";
...etc.
,然後在代碼中使用它:
var shippingShipCalculatorRecords =
_shippingShipCalculatorService.GetAllShippingShipCalculatorRecords()
.Where(scr => (
(scr.CountryId == 0 && Condition)
)
.OrderBy(x => x.Sequence).ToList();
我試圖將其轉換爲布爾,但它也沒有工作。我用FoxPro工作,並可以輕鬆實現它(&條件)。
您無法從字符串執行代碼。相反,將郵政編碼存儲在字符串中。 – SLaks
使用動態Linq? – dee
看起來它實際上不是重複的(因爲Servy的回答得到了來自OP的正面反饋 - 投票重新開始)。 –