rei*_*ard 7 c# linq extension-methods linq-to-sql
我经常需要通过像领域的限制选择publishStart
,publishEnd
,active
我在几个不同的表中有这些字段.因此,只应选择行
a: active == true;
b: publishStart < now;
c: publishEnd > now;
Run Code Online (Sandbox Code Playgroud)
所以,例如:
db.myTable.SingleOrDefault(a => (a.ID == _theID
//now the active and start-end part:
&& ((a.publishEnd > DateTime.Now) || (a.publishEnd == null))
&& ((a.publishStart <= DateTime.Now) || (a.publishStart == null))
&& a.active == true));
Run Code Online (Sandbox Code Playgroud)
这有点冗长,所以我想知道是否有可能创建一个(扩展名?) - 方法,如:
db.myTable.SingleOrDefault(a => (a.ID == _theID).isActive()
Run Code Online (Sandbox Code Playgroud)
其中isActive()
提供了上述代码段的3行.
我怎么能这样做?有没有更好的方法来清理代码?
Pau*_*ing 13
要定义扩展,您需要一个静态类.您可以将它放在您喜欢的任何名称空间中,只需记住将其包含在您的使用中.
public static class Extensions
{
public static IQueryable<T> Active<T>(this IQueryable<T> source)
where T : YourEntityType
{
return source.Where(a => ((a.publishEnd > DateTime.Now) || (a.publishEnd == null))
&& ((a.publishStart <= DateTime.Now) || (a.publishStart == null))
&& a.active == true);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意YourEntityType
那里.这是用来确保方法是认识的存在publishStart
,publishEnd
和active
.这应该是实现这些字段的类或定义它们的契约(接口).
然后你会这样称呼它:
var item = db.myTable.Active().SingleOrDefault(...);
Run Code Online (Sandbox Code Playgroud)
有关扩展方法的更多信息,请访问:http://msdn.microsoft.com/en-us/library/bb383977.aspx
由于遍布各地的评论很多,我将在这里添加一个关于界面解决方案的简要说明......
在问题中是否存在三个过滤字段的通用实现或定义它们的接口尚不清楚.如果没有,为了上述工作,你不会:
YourEntityType
为YourBaseEntityType
.YourEntityType
为IYourContract
. 归档时间: |
|
查看次数: |
11803 次 |
最近记录: |