リスト (csv ファイルから取得) に多数のカスタム エンティティ レコードがあります。
どのレコードが新しいかを確認し、それらを作成する最良の方法は何ですか?
この場合、等価チェックは単一のテキスト フィールドに基づいていますが、ルックアップと 2 つのテキスト フィールドに基づいて等価チェックが行われている他の場所でも同じことを行う必要があります。
議論のために、私が Account レコードを挿入していたとしましょう。これは私が現在持っているものです:
private void CreateAccounts()
{
var list = this.GetAccounts(); // get the list of Accounts, some may be new
IEnumerable<string> existingAccounts = linq.AccountSet.Select(account => account.AccountNumber); // get all Account numbers in CRM, linq is a serviceContextName variable
var newAccounts = list.Where(account => !existingAccounts.Contains(account.AccountNumber)); // Account numbers not in CRM
foreach (var accountNumber in newAccounts) // go through the new list again and get all the Account info
{
var account = newAccounts.Where(newAccount => newAccount.AccountNumber.Equals(accountNumber)).FirstOrDefault();
service.Create(account);
}
}
これを行うより良い方法はありますか?
リストを繰り返し処理しすぎているようですが、CRM を複数回クエリするよりも優れているに違いありません。
foreach (var account in list)
{
// is this Account already in CRM
// if not create the Account
}