- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我很快就会开始研究 xamarin,并将大量代码从 android studio 的 java 转移到 c#。
在 java 中,我使用自定义类,给定参数条件等,将它们转换为 SQL 语句,然后将结果加载到项目模型中的对象
我不确定 linq 是否是过滤此类数据的更好选择。
例如,当前会发生的事情是这样的
List<Customer> customers = (new CustomerDAO()).get_all()
或者如果我有条件
List<Customer> customers = (new CustomerDAO()).get(new Condition(CustomerDAO.Code, equals, "code1")
现在让我们假设我已经将类转移到 c# 并且我希望做一些类似于第二种情况的事情。
所以我可能会按照以下思路写一些东西:
var customers = from customer
in (new CustomerDAO()).get_all()
where customer.code.equals("code1")
select customer
我知道查询只会在我实际尝试访问客户时执行,但是如果我有多个客户访问权限(假设我稍后使用 4 个 foreach 循环),get_all 方法会被调用 4 次吗?还是在第一次执行时存储结果?
仅保留 get_all() 方法并使用 linq 过滤结果是否更有效(时间方面因为内存方面可能不是)?或者使用我现有的设置,它实际上执行了
Select * from Customers where code = 'code1'
并将结果加载到一个对象中?
在此先感谢您提供的任何帮助
编辑:是的,我确实知道有 sqlite.net,它几乎可以做我的 daos 所做的事情,但可能更好,并且在某些时候我可能会转换我所有的对象来使用它,我只需要知道是为了知道
最佳答案
if I have multiple accesses to customers ( let us say that I use 4 foreach loops later on) will the get_all method be called 4 times? or are the results stored at the first execution?
每次您枚举枚举器(在您的示例中使用 foreach
)时,查询将重新执行,除非您将具体化结果存储在某处。例如,如果您在第一个查询中执行以下操作:
var customerSource = new CustomerDAO();
List<Customer> customerSource.Where(customer => customer.Code.Equals("code1")).ToList();
那么现在您将使用内存中的 List<Customer>
无需再次执行查询。
相反,如果每次你都这样做:
var filteredCustomers = customerSource.Where(customer => customer.Code.Equals("code1"))
foreach (var customer in filteredCustomers)
{
// Do stuff
}
然后对于每个枚举,您将再次执行上述查询。
Also is it more efficient (time wise because memory wise it is probably not) to just keep the get_all() method and use linq to filter the results? Or use my existing setup which in effect executes
这实际上取决于您的用例。假设您正在使用 LINQ to EF,并且客户表有一百万行,您真的想要将所有行都放入内存中,然后过滤掉它们以使用数据子集吗?完全过滤查询通常会更好。
关于c# - linq实际上是如何执行代码从数据源中获取数据的呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36823424/
渐进增强和优雅降级基本是一回事吗? 最佳答案 不完全是。他们从不同的 Angular 解决类似的问题。 “优雅的降级”意味着你有漂亮的功能,并且可以在不支持它的浏览器中处理它不那么漂亮(但仍然需要它以
在过去的几周里,我一直在调优和处理 PostgreSQL,我将在我的下一个项目中使用它。 我的规范是: DigitalOcean 8 核 16GB SSD x2(一个用于数据库,另一个用于 Web)
我看过很多关于负数模的问题的答案。每一个答案都放了标准 (a/b)*b + a%b is equal to a 解释。我可以用这种方法计算任何模数,而且我知道有必要使用一个模数函数,如果它是负数,则将
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
The docs假设所有标签都存储在 .hgtags 中,但这里显然存在一些黑魔法。 我的标签如下所示: mbayazit:~/test$ cat .hgtags 0d80b6ba4ba3b51a44
我正在尝试强制删除待处理的更改列表。所有文件(20 个旧文件)都是新文件,但尚未提交/提交。所以在 p4Win 中,它们显示红色 + 十字。我无法从更改列表中删除这些文件。我该如何删除这些文件? 感谢
如果我要删除的文件不属于工作区,那么如何从工作区的目录中删除文件? 我的文件系统上有一个目录,其中包含从 perforce 获取的文件,但在某些进程运行后,它会在这些目录中创建一些新文件。 是否有 p
就是好奇这个。以下是同一功能的两个代码片段: void MyFunc1() { int i = 10; object obj = null; if(something) ret
我对使用约束布局还很陌生,我在调整布局大小方面遇到了问题,我希望它能够响应,这样我就不必再为不同的屏幕尺寸制作 10 个布局。在布局编辑器中,一切在不同尺寸下看起来都很完美,但实际上并非如此。 我做了
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
如果试图修改声明为 const 的对象,const 类型限定符会导致编译器发出错误消息,但这还不够保护。例如以下程序修改声明为 const 的数组的两个元素: #include int main(v
我不得不问这个,因为:我唯一知道的是,如果断言失败,应用程序就会崩溃。这就是为什么要使用 NSAssert 的原因吗?或者这样做还有什么好处?将 NSAssert 置于我在代码中所做的任何假设之上是否
我正在处理我的操作系统项目的 POSIX 子系统,并且我已经达到了我想要处理 pthreads 支持的地步。但是,我不确定我应该在多大程度上实现它们。 最常用的 pthreads 功能是什么?现在有什
这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the h
我正在尝试运行测试类,但抛出错误实际上有零交互。 class Xtractor{ void extractValues(request,Map m1, Map m2,Map m3){
我有一个抽象类UIObject,如下所示: public abstract class UIObject { private final int tabOrder; public UI
这是我尝试在 emacs lisp 中进行一些计算时得到的... (+ 2082844800. 1274511600.0) => 1209872752.0 (+ 2082844800.0 127451
我想用一条垂直线将屏幕分成两部分。垂直线应该从屏幕底部一直延伸到导航栏。如果我们使用 html/css,我只会有 2 个 div,并在右侧 div 上放置一个左边框。如果有办法在 View 的单侧放置
我有一个EC2实例可以正常工作数月(仍在开发中,应用程序尚未启用),但是我只是意识到我什至不知道如何根据流量来扩大/缩小EC2实例。 亚马逊提供的大量服务是压倒性的,我对此感到非常困惑。 最初,虽然我
考虑这个代码: int i = 1; int x = ++i + ++i; 我们对编译器可能会为这段代码做些什么有一些猜测,假设它可以编译。 两者 ++i返回 2 ,导致 x=4 . 一 ++i返回
我是一名优秀的程序员,十分优秀!