3

検索語が文字列値である 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;
    }
4

1 に答える 1

0

Intersect is actually Enumerable.Intersect and is returning an IEnumerable<ZoneResult>. This is not castable to an ObservableCollection because it isn't one - it is the enumeration of the intersecting elements in both collections.

You can, however create a new ObservableCollection from the enumeration:

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

Depending on how many elements you have, how ZoneResult.Equals is implemented, and how many search terms you expect, this implementation may or may not be feasable (FindZones does seem a little overly-complicated with O(n^4) at first glance). If it seems to be a resource hog or bottleneck, it's time to optimize; otherwise I would just leave it alone if it works.


One suggested optimization could be the following (incorporating @Keith's suggestion to change ContainsMember to a bool) - although it is untested, I probably have my SelectManys wrong, and it really largely amounts to the same thing, you hopefully get the idea:

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);
}
于 2012-10-01T16:15:23.740 に答える