LINQ返回空集合,其中ForEach找到一个价值

时间:2017-01-24 23:49:41

标签: c# linq

我有这个代码。我已经对这些课程进行了一些编辑。我已经验证这一切都按预期工作,只有LINQ失败。

public class SANSwitch
{
 public bool HasWWN(string wwn)

     {
        bool test = false;
        if (wwn.StartsWith("55")) { test =    VirtualWWNList.Values.Contains(wwn); }
        else { test= SwitchWWPN.Contains(wwn.Substring(0,20)); }

        return test;
     }
}

 public class SANFabric
{
    // dictionary of switch WWPNs and SANSwitch objects
    public Dictionary<string, SANSwitch> MemberSwitches = new Dictionary<string, SANSwitch>();

    public bool IsFabricMember(string wwn)
    {
        var found = MemberSwitches.Values.Where(t => t.HasWWN(wwn)).ToList();
        if (found.Count() > 0) { return true; }
        else { return false; }
    }
}

List<SANFabric> Fabrics = new List<SANFabric();
string wwn = String.Empty;
 // pretend there's code here and we have a list of fabrics
 // and wwn has been assigned a value

这里有什么用。也就是说,具有与字符串匹配的WWN的交换机是结构的成员,测试器设置为true

bool tester = false;
        //find out if the switch is a member of the fabric

        {
            foreach (SANFabric f in fabrics)
            {
                tester = f.IsFabricMember(wwn);
            }

        }

这里有什么不起作用。 ismember总是空的,即使它是针对同一个集合运行的。

var ismember = fabrics.Where(t => t.IsFabricMember(wwn));

其中IsMember()定义为

  public bool IsFabricMember(string wwn)
    {
        var found = MemberSwitches.Values.Where(t => t.HasWWN(wwn)).ToList();
        if (found.Count() > 0) { return true; }
        else { return false; }
    }

IsMember()总是返回false,因为found.Count始终为0.我已经逐步完成调试器并且HasWWN正常工作。

2 个答案:

答案 0 :(得分:0)

public bool IsFabricMember(string wwn)
{
    if (MemberSwitches.Where(x => x.Value.HasWWN(wwn)).Count() > 0) 
        return true; 
    else 
        return false; 
}

public bool IsFabricMember(string wwn)
{
    if (MemberSwitches.Any(x => x.Value.HasWWN(wwn))) { return true; }
    else { return false; }
}

答案 1 :(得分:0)

根据我填充缺失部分的方式,您的代码可以正常工作。

class Program
    {
        static void Main(string[] args)
        {
            List<SANFabric> Fabrics = new List<SANFabric>() ;
            Dictionary<string,SANSwitch> _swDict=new Dictionary<string,SANSwitch>();
            _swDict.Add("A", new SANSwitch() { SwitchWWPN = new List<string>() { "CDEFGHIJKLMNOPQRSTUVWXYZ", "CDEFGHIJKLMNOPQRSTUVWXYZ12333" }, 
                                                VirtualWWNList = new Dictionary<string, string>() { 
                                                                                                    {"ABC","55555555"},
                                                                                                    {"DEF","993383838"}
                                                                                                  } 
                                             }
                                             );

            _swDict.Add("B", new SANSwitch()
                                            {
                                                SwitchWWPN = new List<string>() { "KGBIFBGKLMPQUERREE", "CDEFGHIKGBIFBGKLMPQUERREEXYZ12333" },
                                                VirtualWWNList = new Dictionary<string, string>() { 
                                                                                                                                    {"GHI","8888383"},
                                                                                                                                    {"JKL","00933939"}
                                                                                                                                  }
                                            });

            Fabrics.Add(new SANFabric()
            {
                MemberSwitches = _swDict
            });

            var f1 = Fabrics.Where(t => t.IsFabricMember("55555555")).ToList();
            var f2 = Fabrics.Where(t => t.IsFabricMember("CDEFGHIJKLMNOPQRSTUV")).ToList();
            Console.WriteLine("# of items to be member: {0},{1}",f1.Count,f2.Count);
            Console.ReadKey();
        }
    }
    public class SANSwitch
    {        
        public Dictionary<string,string> VirtualWWNList {get;set;}

        public List<string> SwitchWWPN {get;set;}

        public bool HasWWN(string wwn)
        {
            bool test = false;
            if (wwn.StartsWith("55")) { test = VirtualWWNList.Values.Contains(wwn); }
            else { test = SwitchWWPN.Contains(wwn.Substring(0, 20)); }

            return test;
        }
    }

    public class SANFabric
    {
        // dictionary of switch WWPNs and SANSwitch objects
        public Dictionary<string, SANSwitch> MemberSwitches { get; set; }

        public bool IsFabricMember(string wwn)
        {
            var found = MemberSwitches.Values.Where(t => t.HasWWN(wwn)).ToList();
            if (found.Count() > 0) { return true; }
            else { return false; }
        }
    }