【发布时间】:2015-06-22 23:01:49
【问题描述】:
我正在尝试找到一种方法来构建 where 子句并将其传递给存储库 Get() 方法。它应该过滤名称以特定字母开头的项目。我能够构建此 Where 子句正文的一部分,但找不到如何处理项目名称不以字母开头的场景的方法。例如:_ItemName 或 97_SomeName。
所以,这是我的方法:
protected override Expression<Func<DataSetSettings, bool>> GetWhereClause()
{
//The user has selected FilterLetter, for example: "A"
// return all items which name starts with "A"
if (!string.IsNullOrWhiteSpace(FilterLetter) && !FilterLetter.Equals("All"))
return (x => x.Name.StartsWith(FilterLetter) && x.Type == Type);
if (FilterLetter.Equals("Other"))
{
//Here i need to extract all items which name does not start with letter
}
//return All items of the current type
return x => x.Type == Type;
}
如果有任何帮助,我将不胜感激!谢谢!
【问题讨论】:
-
您是使用 Linq to SQL 还是实体框架,还是用于内存收集?
-
我正在使用实体框架。
-
你是不是先尝试了一些东西并得到了错误?我认为 Entity 没有
StartsWith等价物。 -
我环顾了一圈,找不到任何关于 EF 中复杂模式匹配的有用信息。我在下面发布了一个建议的答案。
标签: c# linq entity-framework collections