无法使用使用。除两个名单,LT;弦乐> [英] Unable to use use .Except on two List<String>

查看:243
本文介绍了无法使用使用。除两个名单,LT;弦乐>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个asp.net MVC-5的Web应用。我有这两个模型类: -

 公共类ScanInfo
    {
        公共TMSServer TMSServer {集;得到; }
        公共资源资源{集;得到; }
        公开名单< ScanInfoVM> VMList {设置;得到; }
    } 公共类ScanInfoVM
    {
        公共TMSVirtualMachine TMSVM {集;得到; }
        公共资源资源{集;得到; }
    }

和我有以下方法: -

 列表< ScanInfo> scaninfo =新的List< ScanInfo>();    清单<串GT; CurrentresourcesNames =新的List<串GT;();
    的for(int i = 0; I< results3.Count;我++)//循环遍历返回VM名称
         {
            VAR VMNAME =结果3 [I] .BaseObject == NULL?结果3 [I] .Guest.HostName:结果3 [I] .BaseObject.Guest.HostName; //获取名称            如果(!String.IsNullOrEmpty(VMNAME))
                 {
                   如果(scaninfo.Any(一个= GT; a.VMList.Any(A2 = GT; a2.Resource.RESOURCENAME.ToLower()== vmname.ToLower())))                       {                         CurrentresourcesNames.Add(VMNAME);
                       }                  }        }
  变种allcurrentresourcename = scaninfo.Select(一个= GT; a.VMList.Select(A2 = GT; a2.Resource.RESOURCENAME))。了ToList();
  变种finallist = allcurrentresourcename.Except(CurrentresourcesNames).ToList();

现在我希望得到所有可在 allcurrentrecoursename 名单内,但不是里面的字符串 CurrentresourcesName <? / p>

但高于code是提高下列情况除外: -


  

错误4'System.Collections.Generic.List>
  不包含'除了'的定义和最佳推广
  方法重载
  System.Linq.Queryable.Except(System.Linq.IQueryable,
  System.Collections.Generic.IEnumerable)'有一些无效
  参数


  
  

错误3实例参数:无法从转换
  System.Collections.Generic.List>
  以System.Linq.IQueryable



解决方案

在我看来像

  VAR allcurrentresourcename = scaninfo.Select(A =&GT; a.VMList.Select(A2 =&GT; a2.Resource.RESOURCENAME))了ToList()。

不是一个字符串列表都不像你似乎希望它是。 scaninfo 的类型为列表与LT; ScanInfo&GT; 和拉姆达前pression

  A =&GT; a.VMList.Select(A2 =&GT; a2.Resource.RESOURCENAME)

产生有一个的IEnumerable&LT; TSomething&GT; 每个 ScanInfo 对象。因此,它似乎 allcurrentresourcename 不是列表&LT;串&GT; ,而在名单,LT ; IEnumerable的&LT; TSomething&GT;&GT; ,其中 TSomething 资源名称的类型(最可能字符串)。

编辑:你现在presumably想在这里用的是的SelectMany LINQ方法(见@ pquest的评论)。它展平,你能资源名称,然后您可以使用除了上的一大名单列表:

  VAR allcurrentresourcename = scaninfo.SelectMany(A =&GT; a.VMList.Select(
    B =&GT; b.Resource.RESOURCENAME));

您应该甚至不需要了ToList()在该行的末尾。

I am working on an asp.net mvc-5 web applicatio. 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 ?

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

Error 3 Instance argument: cannot convert from 'System.Collections.Generic.List>' to '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

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

yields one IEnumerable<TSomething> for each ScanInfo object. 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).

Edit: What you presumably want to use here is the SelectMany LINQ method (see @pquest's comment). It flattens the lists that you get to "one big list" of resource names, which you can then use Except on:

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.

这篇关于无法使用使用。除两个名单,LT;弦乐&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆