Except() 和 Intersect() 无法正常工作 - LINQ
Except() and Intersect() doesn't work correctly - LINQ
我不太确定为什么这不能正常工作。是否可能是因为我试图在数据 class 上使用表达式而不是 class 中的单个字段?
adminOnlyGroups
应该 return 管理员拥有而用户没有的群组,但它正在 return 所有 admin 组
userOnlyGroups
也在做同样的事情,它正在 return 所有 user groups
commonGroups
应该 return 他们有两个共同的组,但它是 returning null 或 empty
数据Class
[DataContract]
public class InvestigatorGroupData
{
[DataMember]
public int InvestigatorGroupId { get; set; }
[DataMember]
public string InvestigatorGroupName { get; set; }
}
我的控制器的片段
IEnumerable<InvestigatorGroupData> adminGroups = proxy.GetInvestigatorGroups(adminId);
IEnumerable<InvestigatorGroupData> userGroups = proxy.GetInvestigatorGroups(userId);
// Groups that admin has and user doesn't. These will be enabled and unselected
IEnumerable<InvestigatorGroupData> adminOnlyGroups = adminGroups.Except(userGroups);
// Groups that the user has and admin doesn't. These will be disabled and selected
IEnumerable<InvestigatorGroupData> userOnlyGroups = userGroups.Except(adminGroups);
// Groups in common. These will be enabled and selected
IEnumerable<InvestigatorGroupData> commonGroups = adminGroups.Intersect(userGroups);
if (commonGroups.IsNullOrEmpty())
{
return View("Error");
}
您需要为对象实现相等比较器
之前已经在这里回答过
Using Linq Except not Working as I Thought
请参阅下面来自 MSDN 的博客 post 以获取更多说明
http://blogs.msdn.com/b/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx
如果您是匿名类型,则在没有相等比较器的情况下它的工作方式会有所不同。这是一个示例代码
http://odetocode.com/blogs/scott/archive/2008/03/25/and-equality-for-all-anonymous-types.aspx
我不太确定为什么这不能正常工作。是否可能是因为我试图在数据 class 上使用表达式而不是 class 中的单个字段?
adminOnlyGroups
应该 return 管理员拥有而用户没有的群组,但它正在 return 所有 admin 组
userOnlyGroups
也在做同样的事情,它正在 return 所有 user groups
commonGroups
应该 return 他们有两个共同的组,但它是 returning null 或 empty
数据Class
[DataContract]
public class InvestigatorGroupData
{
[DataMember]
public int InvestigatorGroupId { get; set; }
[DataMember]
public string InvestigatorGroupName { get; set; }
}
我的控制器的片段
IEnumerable<InvestigatorGroupData> adminGroups = proxy.GetInvestigatorGroups(adminId);
IEnumerable<InvestigatorGroupData> userGroups = proxy.GetInvestigatorGroups(userId);
// Groups that admin has and user doesn't. These will be enabled and unselected
IEnumerable<InvestigatorGroupData> adminOnlyGroups = adminGroups.Except(userGroups);
// Groups that the user has and admin doesn't. These will be disabled and selected
IEnumerable<InvestigatorGroupData> userOnlyGroups = userGroups.Except(adminGroups);
// Groups in common. These will be enabled and selected
IEnumerable<InvestigatorGroupData> commonGroups = adminGroups.Intersect(userGroups);
if (commonGroups.IsNullOrEmpty())
{
return View("Error");
}
您需要为对象实现相等比较器
之前已经在这里回答过 Using Linq Except not Working as I Thought
请参阅下面来自 MSDN 的博客 post 以获取更多说明 http://blogs.msdn.com/b/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx
如果您是匿名类型,则在没有相等比较器的情况下它的工作方式会有所不同。这是一个示例代码
http://odetocode.com/blogs/scott/archive/2008/03/25/and-equality-for-all-anonymous-types.aspx