如何使用多个值检查CONTAINS

时间:2012-10-01 16:05:43

标签: c# linq

我正在尝试查找包含2个或更多区域成员的所有区域,其中搜索项是字符串值。这是我的代码。在FindCommmonZones方法中,当我尝试将Intersect的结果转换为ObservableCollection时,我得到了无效转换的运行时。问题是,有更好的方法吗?作为FindCommonZones()的参数的字符串数组可以是任何字符串数。 StackOverflow还有一些其他类似的帖子,但没有一个真正回答我的问题 - 看起来它们都与SQL有关。

一些代码:

public class Zone
{ 
    public List<ZoneMember> MembersList = new List<ZoneMember>();
    private string _ZoneName;
    public string zoneName{ get{return _ZoneName;}  set{_ZoneName=value;} }
        public Zone ContainsMember(string member)
    {
      var contained = this.MembersList.FirstOrDefault(m => m.MemberWWPN.
             Contains(member) || m.MemberAlias.Contains(member));

      if (contained != null) { return this; }
      else { return null; }

    }

}

 public class ZoneMember
    // a zone member is a member of a zone
    // zones have ports, WWPNs, aliases or all 3
{
    private string _Alias = string.Empty;
    public string MemberAlias {get{return _Alias;} set{_Alias = value; } }
    private FCPort _Port = null;
    public FCPort MemberPort { get { return _Port; } set { _Port = value; } }
    private string _WWPN = string.Empty;
    public string MemberWWPN { get { return _WWPN; } set { _WWPN = value; } }
    private bool _IsLoggedIn;
    public bool IsLoggedIn { get { return _IsLoggedIn; } set { _IsLoggedIn = value; } }
    private string _FCID;
    public string FCID {get{return _FCID;} set{ _FCID=value; } }
}


private ObservableCollection<ZoneResult> FindCommonZones(string[] searchterms)
    {

        ObservableCollection<ZoneResult> tempcollection = 
          new ObservableCollection<ZoneResult>();
        //find the zones for the first search term
        tempcollection = this.FindZones(searchterms[0]);

        //now search for the rest of the search terms and compare 
         //them to existing result
        for (int i = 1; i < searchterms.Count(); i++ )
        {  
           // this line gives an exception trying to cast
           tempcollection = (ObservableCollection<ZoneResult>)tempcollection.
             Intersect(this.FindZones(searchterms[i]));

        }

        return tempcollection;
    }
    private ObservableCollection<ZoneResult> FindZones(string searchterm)
    // we need to track the vsan where the zone member is found
    // so use a foreach to keep track
    {
        ObservableCollection<ZoneResult> zonecollection = new ObservableCollection<ZoneResult>();
        foreach (KeyValuePair<int, Dictionary<int, CiscoVSAN>> fabricpair in this.FabricDictionary)
        {
            foreach (KeyValuePair<int, CiscoVSAN> vsanpair in fabricpair.Value)
            {
                var selection = vsanpair.Value.ActiveZoneset.
                           ZoneList.Select(z => z.ContainsMember(searchterm)).
                               Where(m => m != null).OrderBy(z => z.zoneName);
                if (selection.Count() > 0)
                {

                    foreach (Zone zone in selection)
                    {
                        foreach (ZoneMember zm in zone.MembersList)
                        {
                            ZoneResult zr = new ZoneResult(zone.zoneName, 
                            zm.MemberWWPN, zm.MemberAlias, vsanpair.Key.ToString());
                            zonecollection.Add(zr);
                        }

                    }

                }
            }

        }
        return zonecollection;
    }

1 个答案:

答案 0 :(得分:0)

Intersect实际上是Enumerable.Intersect,正在返回IEnumerable<ZoneResult>。这不是 castable 到ObservableCollection,因为它不是一个 - 它是两个集合中相交元素的枚举。

但是,您可以从枚举中创建 new ObservableCollection:

tempcollection = new ObservableCollection<ZoneResult>(tempcollection
    .Intersect(this.FindZones(searchterms[i]));

根据您拥有的元素数量,ZoneResult.Equals的实施方式以及您期望的搜索字词数量,此实施可能会或可能不会可行(FindZones似乎有点过于复杂O(n ^ 4)乍一看)。如果它似乎是资源匮乏或瓶颈,那么是时候进行优化了;如果它有效,我会不管它。


一个建议的优化可能是以下(将@ Keith的建议纳入将ContainsMember更改为bool) - 虽然它未经测试,但我可能让我的SelectManys错了,它实际上很大程度上是相同的,你希望得到这个想法:

private ObservableCollection<ZoneResult> FindCommonZones(string[] searchterms)
{

    var query = this.FabricDictionary.SelectMany(fabricpair => 
        fabricpair.Value.SelectMany(vsanpair => 
            vsanpair.Value.ActiveZoneSet.ZoneList
            .Where(z=>searchterms.Any(term=>z.ContainsMember(term)))
            .SelectMany(zone => 
                zone.MembersList.Select(zm=>new ZoneResult(zone.zoneName, zm.MemberWWPN, zm.MemberAlias, vsanpair.Key.ToString()))
        )
    )
    .Distinct()
    .OrderBy(zr=>zr.zoneName);

    return new ObservableCollection<ZoneResult>(query);
}