LINQ 中的Where IN 子句(改进方案)
在 SQL 中,WHERE IN 子句用于检查字段是否与任何值匹配从列表中。您可以使用以下改进的方法在 LINQ 中实现类似的功能。
public List<State> Wherein(string listofcountrycodes) { string[] countryCode = listofcountrycodes.Split(','); var states = from states in _objdatasources.StateList() where countryCode.Contains(states.CountryCode) select new State { StateName = states.StateName }; return states.ToList(); }
此代码使用 LINQ Contains 方法来检查某个州的 CountryCode 是否与 CountryCode 数组中的任何值匹配。 ToList 方法用于将 states 查询转换为具体的列表。
说明:
以上是如何在 LINQ 中有效地使用等效的 WHERE IN 子句?的详细内容。更多信息请关注PHP中文网其他相关文章!