简体   繁体   English

如何在 List 中转换对象类型<t> 。除了

[英]How to cast the object type in List<t>.Except

I made two simple classes for user and users, in class user I only have userID, userName and List<string> userGroup , and in class users I have the member for two user and two properties to compare the userGroup.我为用户和用户创建了两个简单的类,在用户类中我只有 userID、userName 和List<string> userGroup ,在用户类中我有两个用户的成员和两个属性来比较用户组。

        public List<string> DifferenceSetAtoB
        {
            get
            {
                return (List<string>)UserA.UserGroups.Except((List<string>)UserB.UserGroups);
            }
        }

what I'm trying to do is to use the Except method to return the difference set between A and B.我想要做的是使用 except 方法返回 A 和 B 之间的差异集。

but when I run the code, I will get the error message on the return line saying:但是当我运行代码时,我会在返回行收到错误消息:

System.InvalidCastException HResult=0x80004002 Message=Unable to cast object of type 'd__81 1[System.String]' to type 'System.Collections.Generic.List 1[System.String]'. System.InvalidCastException HResult=0x80004002 消息=无法将类型“d__81 1[System.String]' to type 'System.Collections.Generic.List 1[System.String]”。

My understanding is that the data type of UserB.UserGroups is List and when I use Except it's a method from Collection so the data type is Collections.General.List.我的理解是UserB.UserGroups的数据类型是List,当我使用Except时它是Collection中的一个方法,所以数据类型是Collections.General.List。 But I don't know how to force the data type to be the same.但我不知道如何强制数据类型相同。 Isn't List is already from System.Collections.Generic? List 不是已经来自 System.Collections.Generic 了吗? Can anyone please help?有人可以帮忙吗?

Full code below:完整代码如下:

    public class User
    {
        public string UserId { get; set; }
        public string UserName { get; set; }
        public List<string> UserGroups { get; set; }

        public User()
        {
            this.UserGroups = new List<string>();
        }
    }

    public class UserComparison
    {
        public User UserA { get; set; }
        public User UserB { get; set; }

        public List<string> DifferenceSetAtoB
        {
            get
            {
                return (List<string>)UserA.UserGroups.Except((List<string>)UserB.UserGroups);
            }
        }
        public List<string> DifferenceSetBtoA
        {
            get
            {
                return (List<string>)UserB.UserGroups.Except((List<string>)UserA.UserGroups);
            }
        }

        public UserComparison()
        {
            this.UserA = new User();
            this.UserB = new User();
        }
    }

Except(...) returns IEnumerable<> so the cast is invalid. Except(...)返回IEnumerable<>因此强制转换无效。 If you want to return a list, you can use ToList() method:如果要返回列表,可以使用ToList()方法:

return UserA.UserGroups.Except(UserB.UserGroups).ToList();

As it might take some time to process, it should not be a property.由于可能需要一些时间来处理,它不应该是财产。 Though it is just a matter of design choice.虽然这只是设计选择的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM