3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
這個(gè)表達(dá)式應(yīng)該做你想要達(dá)到的目標(biāo)。
dataSource.StateList.Where(s => countryCodes.Contains(s.CountryCode))

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
這將轉(zhuǎn)換為L(zhǎng)inq to SQL中的where in子句...
var myInClause = new string[] {"One", "Two", "Three"};
var results = from x in MyTable
where myInClause.Contains(x.SomeColumn)
select x;
// OR
var results = MyTable.Where(x => myInClause.Contains(x.SomeColumn));
在您的查詢的情況下,您可以做這樣的事情......
var results = from states in _objectdatasource.StateList()
where listofcountrycodes.Contains(states.CountryCode)
select new State
{
StateName = states.StateName
};
// OR
var results = _objectdatasource.StateList()
.Where(s => listofcountrycodes.Contains(s.CountryCode))
.Select(s => new State { StateName = s.StateName});

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
我喜歡它作為擴(kuò)展方法:
public static bool In<T>(this T source, params T[] list)
{
return list.Contains(source);
}
現(xiàn)在你打電話:
var states = _objdatasources.StateList().Where(s => s.In(countrycodes));
您也可以傳遞單個(gè)值:
var states = tooManyStates.Where(s => s.In("x", "y", "z"));
感覺更自然,更接近sql。
- 3 回答
- 0 關(guān)注
- 640 瀏覽
添加回答
舉報(bào)