从 linq 语句中调用函数
只是想知道这是否是最有效的方法?有没有一种方法可以将所有 linq 包含在一个语句中,而不是调用方法,例如子选择或其他方法?
newEmployee = (from emp
in db.employees
select new
{
a.EmployeeID,
a.Username,
Status = emp.GetEmployeeCurrentStatus(a.Username)
}).ToList();
这是返回员工状态的 GetEmployeeCurrentStatus:
public string GetEmployeeCurrentStatus(string username)
{
using (Entities db = new Entities())
{
var times = (from d in db.TimeTables
where d.DateTime == DateTime.Today &&
d.Employee.Username == username
select d)
.OrderByDescending(d => d.TimeID).FirstOrDefault();
return (x.ClockOut == null ? "IN" : "OUT");
}
}
Just wondering if this is the most efficient method of doing this? is there a way of having all the linq within one statement instead of calling a method, like a subselect or something?
newEmployee = (from emp
in db.employees
select new
{
a.EmployeeID,
a.Username,
Status = emp.GetEmployeeCurrentStatus(a.Username)
}).ToList();
This is the GetEmployeeCurrentStatus which returns the status of the employee:
public string GetEmployeeCurrentStatus(string username)
{
using (Entities db = new Entities())
{
var times = (from d in db.TimeTables
where d.DateTime == DateTime.Today &&
d.Employee.Username == username
select d)
.OrderByDescending(d => d.TimeID).FirstOrDefault();
return (x.ClockOut == null ? "IN" : "OUT");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
怎么样:
您的尝试可能看起来更干净并且功能正常。但是,它正在启动辅助数据库调用。这将不利于可扩展性和性能。我发布的版本使用相同的初始数据库连接,并且将使连接成为 1-1。这将导致更严格、更快的查询以及更低的资源使用。
how about:
Your attempt may appear cleaner and is functionally ok. However, it is firing up a secondary db call. This will be bad for scalability and performance. The version i've posted uses the same initial db connection and will make the join 1-1. This will result in tighter, faster queries as well as lower resource usage.
您无法真正在查询(或将使用数据库执行的查询的一部分)内调用自定义方法。您基本上有两个选择:
在执行需要调用该方法的
select
之前调用ToList
(这样,将在内存数据上调用该方法)编写如果可能的话,查询可以全部在 SQL Server 上运行。这可以使用 谓词生成器 中的
AsExpandable
扩展来完成。有关其工作原理的详细信息,另请参阅我的博客文章。You cannot really call a custom method inside a query (or a part of a query that will be executed using the databse). You have essentially two options:
Call
ToList
before performing theselect
that needs to call the method (this way, the method will be called on in-memory data)Compose the query such that it can all run on the SQL server if it is possible. This can be done using
AsExpandable
extension in predicate builder. For more information on how this works, see also my blog post.对于小数据(员工计数)来说这很好,但由于每个 GetEmployeeCurrentStatus 都需要一个 sql 新连接,因此这不是最佳实践。
我个人将获取所有员工(一次数据库访问),然后获取所有员工状态(一次数据库访问),所以我将它们全部兑现,现在我将在本地加入他们
希望这有帮助
its fine for small data (employees count) but since each
GetEmployeeCurrentStatus
requires an sql new connection so its not that best practice.I personally will get all employees (one trip to database) and then get all employees status (one trip to database) so i cashed them all, now i'll join them locally
Hope this helped
不管效率如何,使用 GetEmployeeCurrentStatus(...) 作为方法会使代码更清晰且更可重用。
Regardless of efficiency, having GetEmployeeCurrentStatus(...) as a method makes the code clearer and more reusable.
假设您使用 LINQ to SQL 或 EF,我将重构您的查询以使用
Join
。这样,您将在数据库上执行单个高效的 SQL 查询,而不是两个单独的查询。Assuming you are using LINQ to SQL or EF, I would refactor your query to use a
Join
. That way, you will execute a single efficient SQL query on the database, instead of two separate queries.