如何加速C#/ Linq查询? [我不需要获取数据,我需要得到条件]
发布时间:2020-12-15 19:26:36 所属栏目:百科 来源:网络整理
导读: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 ==
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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |