c# - 如何加速 C#/Linq 查询? [我不需要获取数据,我需要获取条件]

标签 c# .net entity-framework linq linq-to-entities

    public int being = 0;
    public void Insert(Currency current, int number)
    {
        being = db.Currency.Where(x => x.ForDate == current.ForDate)
            .Where(x => x.TitleId == current.TitleId)
            .Where(x => x.Value == current.Value).Count(x=>x.Id > 0);
        if (being == 0)
        {
            db.Currency.AddOrUpdate(current);
        }
   }

这是我的代码运行如此缓慢的原因,因为获取日期但这不是必需的,我不知道其他方式。 也许是这样的:

db.Currency.Find().Value.Equals(current.Value).where...where...

最佳答案

我认为你的主要问题是 .Count(x => x.Id > 0),它强制评估之前的所有条件并实际获得总数。

如果可以,请将其替换为Any。这样,它最多只需要一行:

bool isBeing = db.Currency
               .Where(x => x.ForDate == current.ForDate
                           && x.TitleId == current.TitleId
                           && x.Value == current.Value
                           && x.Id > 0
                     )
               .Any();

关于c# - 如何加速 C#/Linq 查询? [我不需要获取数据,我需要获取条件],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34490588/

相关文章:

c# - Asp.Net MVC - 在数据库中插入多行

c# - .NET 中不同线程之间共享的列表

.net - 使用什么策略在大型数据集上实现预先输入自动完成?

asp.net-mvc - 多对多关系、复选框的 CRUD View

entity-framework - Entity Framework 4 Code-First优缺点

c# - 为什么这个 LINQ 连接语句不起作用?

c# - 将 TransformedBitmap 对象保存到磁盘。

C#:询问用户密码,然后将其存储在 SecureString 中

c# - 在 foreach 循环中转换

.net - 如何将 'ServerCertificateValidationCallback' 属性设置回其默认行为?