gpt4 book ai didi

c# - 是否可以在 Linq to SQL 中使用自动过滤器?

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:13 25 4
gpt4 key购买 nike

我正在开发一个客户决定使用状态记录的系统。其中之一是 X,表示排除。我想知道的是,是否可以运行添加类似内容的 linq 查询

where status != 'X'

自动不显示“排除的”记录。谢谢!

最佳答案

有点。 Linq 中的查询是延迟求值的,因此您可以在实际获取第一个结果之前根据需要向它们附加条件,它仍然会导致使用“最佳”SQL。

例如:

// an extension method on the LINQ context:
public static IQueryable<Story> FilteredStories(this DbContext db)
{
return from story in db.Stories where status != "X" select story;
}

// ...later...
var stories = from story in db.FilteredStories()
where title = "Something"
select story;
foreach(var story in stories)
{
// whatever...
}

您还可以“隐藏”底层的 LINQ 上下文,并始终通过附加 status != "X" 条件的包装类。当然,问题是如果您想要过滤列表,那么您将不得不跳过箍...

关于c# - 是否可以在 Linq to SQL 中使用自动过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3756405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com