无法使用使用。除两个名单,LT;弦乐&GT; [英] Unable to use use .Except on two List<String>
问题描述
我工作的一个asp.net MVC-5的Web应用。我有这两个模型类: -
公共类ScanInfo
{
公共TMSServer TMSServer {集;得到; }
公共资源资源{集;得到; }
公开名单&LT; ScanInfoVM&GT; VMList {设置;得到; }
} 公共类ScanInfoVM
{
公共TMSVirtualMachine TMSVM {集;得到; }
公共资源资源{集;得到; }
}
和我有以下方法: -
列表&LT; ScanInfo&GT; scaninfo =新的List&LT; ScanInfo&GT;(); 清单&LT;串GT; CurrentresourcesNames =新的List&LT;串GT;();
的for(int i = 0; I&LT; 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;
和拉姆达前pressionA =&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 theCurrentresourcesName
?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 typeList<ScanInfo>
, and the lambda expressiona => a.VMList.Select(a2 => a2.Resource.RESOURCENAME)
yields one
IEnumerable<TSomething>
for eachScanInfo
object. So it would seem thatallcurrentresourcename
is not aList<string>
, but rather aList<IEnumerable<TSomething>>
, whereTSomething
is the type ofRESOURCENAME
(most likelystring
).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 useExcept
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屋!