1

我有一个组织对象

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

和一个 AccountType 对象

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

我正在寻找一种方法来查看现有组织列表并从每个组织中删除帐户类型,其中帐户类型在另一个帐户类型列表中找不到(这将是来自浏览器的回发)。

我会做类似的事情吗?

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?

我正在寻找一个 Linq 查询,它将一个列表与另一个列表进行比较,并仅返回 AccountTypeID 匹配或存在的那些项目。

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 列表的元素列表,同样您可以在代码中应用,首先找出哪些不存在,然后将其从列表中删除

图片 在此处输入图像描述

于 2012-07-24T12:58:17.047 回答