【发布时间】:2013-06-13 12:46:10
【问题描述】:
我有一个带有可选参数的搜索:
string id1 = HttpContext.Current.Request["id1"];
string id2 = HttpContext.Current.Request["id2"];
List<Foo> list = context.Foo.Where(l =>
(string.IsNullOrEmpty(id1) || l.Id1.Contains(id1)) &&
(string.IsNullOrEmpty(id2) || l.Id2.Contains(id2)))
.Take(10)
.ToList());
我想扩展它,如果字符串以* 开头,则应该使用EndsWith() - 方法。
例如,如果第一个搜索字符串是*123,我想做一个l.Id1.EndsWith("123")。
有什么方法可以扩展我当前的代码,还是应该使用不同的方法?
【问题讨论】:
-
我认为除非您有特定的理由使用 StartsWith 或 EndsWith,否则 Contains 会给您更多的灵活性
-
@Stokedout 好吧,我问一个原因:-)