C# - 将IEnumerable转换为Dictionary <object,string>

eri*_*pap 1 c# wpf

我正在建立一个WPF UserControl.为此,我实现了ItemSource DependecyProperty这样的:

private IEnumerable MisItems;
    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(TextBoxAutoComplete), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));

    private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var control = sender as TextBoxAutoComplete;
        if (control != null)
            control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
    }

    private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
    {
        MisItems = newValue;
        // Remove handler for oldValue.CollectionChanged
        var oldValueINotifyCollectionChanged = oldValue as INotifyCollectionChanged;

        if (null != oldValueINotifyCollectionChanged)
        {
            oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
        }
        // Add handler for newValue.CollectionChanged (if possible)
        var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
        if (null != newValueINotifyCollectionChanged)
        {
            newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
        }

    }

    void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        //Do your stuff here.
    }
Run Code Online (Sandbox Code Playgroud)

ItemsSource属性由IEnumerableObject 表示.现在我需要Dictionary<object,string在此函数中将其转换为>:

protected SearchResult DoSearch(string searchTerm)
    {
        if (!string.IsNullOrEmpty(searchTerm))
        {
            SearchResult sr = new SearchResult();
            //var ItemsText = MisItems.GetType();
            var p = (List<string>)MisItems;

             /*sr.Results = ItemsText.Select((x, i) => new { x, i }).Where(x=>x.ToString().ToUpper().Contains(searchTerm.ToUpper()))
            .ToDictionary(a => (object)a.i, a => a.x);*/

            return sr;
        }
        else return new SearchResult();           
    }
Run Code Online (Sandbox Code Playgroud)

我怎样才能过渡?

编辑 更多信息:我的viewmodel有这个属性:

public List<EnumeradorWCFModel> Clientes { get; set; }
Run Code Online (Sandbox Code Playgroud)

此属性的数据由以下内容返回WCF service:

Clientes = _svc.Clientes_Enum(sTicket, "");
Run Code Online (Sandbox Code Playgroud)

然后我希望我UserControl绑定到这个属性.我像这样创建我的控件:

<autocomplete:TextBoxAutoComplete x:Name="Clientes"  ItemsSource = "{Binding Path=Clientes}" DisplayMemberPath="Descripcion" Height="25"/>
Run Code Online (Sandbox Code Playgroud)

Cam*_*ron 8

[S]好的.你发布了很多代码(我个人认为你不想做什么).

让我们减肥吧.

你有一个IEnumerable<string>开始,对吗?好.

ToDictionary()LINQ库中有一个扩展方法.文档在这里.

所以你需要做的是以下几点:

IEnumerable<string> myEnumerableOfStrings = new List<string>();

Dictionary<object, string> dictionary = myEnumerableOfStrings.ToDictionary(value => (object) value);
Run Code Online (Sandbox Code Playgroud)

这里有一个小提琴作为例子.

好吧,所以我们只有一个IEnumerable没有强大的类型.(首先我见过或听说过这个问题,但同样的原则应该适用.)

我们需要创建一个本地字典并迭代该集合.

var myDictionary = new Dictionary<object, string>();
IEnumerable myCollection = new List<string>();

foreach(var item in myCollection)
{
    // This might be fun if you get two of the same object in the collection.
    // Since this key is based off of the results of the GetHashCode() object in the base object class.
    myDictionary.Add((object) item, item.ToString());
}
Run Code Online (Sandbox Code Playgroud)

这是一个例子.