1

組織オブジェクトがあります

public class Organisation
{
    OrgId {get....}
    OrgName {get...}
    AccountTypes { get....} //this is of type List<AccountType>
}

および AccountType オブジェクト

public class AccountType
{
    AccountTypeId {get....}
    AccountTypeName {get...}
}

既存の組織リストを調べて、別の AccountTypes リストにアカウント タイプが見つからない各組織から AccountTypes を削除する方法を探しています (これはブラウザーからのポスト バックになります)。

私は何かをしますか?

var foundOrgs = from org in orgs
                where org.OrganisationId == Convert.ToInt32(hash["orgId"])
                select org;
Organisation organisation = foundOrgs.ElementAt(0);
organisation.AccountTypes.Clear();
organisation.AccountTypes = // What goes here?

あるリストを別のリストと比較し、AccountTypeIDs が一致する、または存在するアイテムのみを返す Linq クエリを実行しようとしています。

4

2 に答える 2

1

使用できますList<T>.RemoveAll

// where accounts is IEnumerable<int>
organisation.AccountTypes.RemoveAll(at => !accounts.Contains(at.AccountTypeId));
于 2012-07-24T12:57:12.497 に答える
1

編集されたコード

//created account id list over here
var AccountTypeID = accountType.Select(x=>x. AccountTypeId);

//you code    
var foundOrgs = from org in orgs
                 where org.OrganisationId == Convert.ToInt32(hash["orgId"])
                 select org;
 Organisation organisation = foundOrgs.ElementAt(0);
 organisation.AccountTypes.Clear();
//changes in code where you want to change -// What goes here? 
List<AccountTypes> templist = organisation.AccountTypes;
 organisation.AccountTypes = 
               from acc in templist 
               where !AccountTypeID.Conains(acc.AccountTypeId)
                       select acc).ToList();

編集

わかりませんが、試してみることができます

var orgdata= from org in foundOrgs
        select { OrgId =org.OrgId ,OrgName = org.OrgName , 
                 AccountTypes  = ( from acc in org.AccountTypes
                                  where !AccountTypeID.Conains(acc.AccountTypeId)
                                  select acc) };

このようなことを試してください

var ids = {1, 2, 3};
  var query = from item in context.items
             where !ids.Contains( item.id )
             select item; 

これにより、1,2,3、つまり ids list の一部ではない要素のリストが得られます。コードに適用できるのと同じで、最初にそこにないものを見つけてから、リストから削除します

画像 ここに画像の説明を入力

于 2012-07-24T12:58:17.047 に答える