[英]Fastest way to check if a record exists in an Oracle Database with Entity Framework 6
[英]Fastest Approach to check if Entity already Exists in Database?
目前,我有類似這樣的內容,以檢查是否已添加(簡化)實體:
WinEveryBetBYeMiContext context = new WinEveryBetBYeMiContext();
string firstPlayerName = "R.Federer";
foreach (var player in context.Players)
{
if (player.Name == firstPlayerName)
{
//Player already exists
}
}
但是由於可能有成百上千的玩家,所以這種方法在我看來非常糟糕,而且也很慢。
我聽說過LINQ TO SQL。。但是我真的不知道它是否更好,更快,更有效。。也許有人可以檢查這種情況並為我提供幫助。
您可以使用LINQ使其更加有效和可讀:
using System.Linq;
var firstPlayerName = "R.Federer";
var context = new WinEveryBetBYeMiContext();
var exists = context.Players.Any(x => x.Name == firstPlayerName);
context.Players.Any(p => p.Name == firstPlayerName);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.