简体   繁体   English

无法使用。除了两个列表<String>

[英]Unable to use use .Except on two List<String>

I am working on an asp.net mvc-5 web applicatio. 我正在asp.net mvc-5 Web应用程序上工作。 i have these two model classes:- 我有这两个模型类:

public class ScanInfo
    {
        public TMSServer TMSServer { set; get; }
        public Resource Resource { set; get; }
        public List<ScanInfoVM> VMList { set; get; }
    }



 public class ScanInfoVM
    {
        public TMSVirtualMachine TMSVM { set; get; }
        public Resource Resource { set; get; }
    }

and i have the following method:- 我有以下方法:

    List<ScanInfo> scaninfo = new List<ScanInfo>();

    List<String> CurrentresourcesNames = new List<String>();


    for (int i = 0; i < results3.Count; i++)//loop through the returned vm names
         {


            var vmname = results3[i].BaseObject == null ? results3[i].Guest.HostName : results3[i].BaseObject.Guest.HostName;//get the name

            if (!String.IsNullOrEmpty(vmname))
                 {
                   if (scaninfo.Any(a => a.VMList.Any(a2 => a2.Resource.RESOURCENAME.ToLower() == vmname.ToLower())))

                       {

                         CurrentresourcesNames.Add(vmname);
                       }   

                  }

        }
  var allcurrentresourcename = scaninfo.Select(a => a.VMList.Select(a2 => a2.Resource.RESOURCENAME)).ToList();
  var finallist = allcurrentresourcename.Except(CurrentresourcesNames).ToList();

now i want to get all the String that are inside the allcurrentrecoursename list but not inside the CurrentresourcesName ? 现在我想一切都是字符串里面allcurrentrecoursename名单,但不是里面CurrentresourcesName

but that above code is raising the following exceptions :- 但是上面的代码引发了以下异常:

Error 4 'System.Collections.Generic.List>' does not contain a definition for 'Except' and the best extension method overload 'System.Linq.Queryable.Except(System.Linq.IQueryable, System.Collections.Generic.IEnumerable)' has some invalid arguments 错误4'System.Collections.Generic.List>'不包含'Except'的定义,最佳扩展方法重载'System.Linq.Queryable.Except(System.Linq.IQueryable,System.Collections.Generic.IEnumerable) '有一些无效的论点

Error 3 Instance argument: cannot convert from 'System.Collections.Generic.List>' to 'System.Linq.IQueryable' 错误3实例参数:无法从“ System.Collections.Generic.List>”转换为“ System.Linq.IQueryable”

It looks to me like 在我看来

var allcurrentresourcename = scaninfo.Select(a => a.VMList.Select(a2 => a2.Resource.RESOURCENAME)).ToList();

is not a list of strings at all like you seem to expect it to be. 根本不像您期望的那样是字符串列表。 scaninfo is of type List<ScanInfo> , and the lambda expression scaninfo的类型为List<ScanInfo> ,并且为lambda表达式

a => a.VMList.Select(a2 => a2.Resource.RESOURCENAME)

yields one IEnumerable<TSomething> for each ScanInfo object. 为每个ScanInfo对象产生一个IEnumerable<TSomething> So it would seem that allcurrentresourcename is not a List<string> , but rather a List<IEnumerable<TSomething>> , where TSomething is the type of RESOURCENAME (most likely string ). 因此,似乎allcurrentresourcename不是List<string> ,而是List<IEnumerable<TSomething>> ,其中TSomethingRESOURCENAME的类型(很可能是string )。

Edit: What you presumably want to use here is the SelectMany LINQ method (see @pquest's comment). 编辑:您大概想在这里使用的是SelectMany LINQ方法(请参阅@pquest的注释)。 It flattens the lists that you get to "one big list" of resource names, which you can then use Except on: 它将您获得的列表简化为资源名称的“一个大列表”,然后可以在以下位置使用“ Except ”:

var allcurrentresourcename = scaninfo.SelectMany(a => a.VMList.Select(
    b => b.Resource.RESOURCENAME));

You shouldn't even need the ToList() at the end of the line. 您甚至不需要在行尾使用ToList()

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

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