无法转换为&#39; System.Collections.Generic.IEnumerable <string>&#39;到了&#39; T&#39;

时间:2015-09-07 19:23:11

标签: c# generics ienumerable

我希望这是一件非常简单的事情,我完全不知道,但我似乎无法弄清楚这里需要改变什么。

  

无法从System.Collections.Generic.IEnumerable<string>转换为T

编译错误来自此块,特别是item.DataItem:

    public List<ImportItem<T>> ProcessReportResult(CSVTable resultData, ICollection<ImportItem<T>> data, Func<T, IEnumerable<string>> keyFilter)
    {
        WriteLog("{1}{0} records found.{1}", resultData.Rows.Length, Environment.NewLine);

        //key = Order Number; value = Order ID
        var idDictionary = resultData.Rows.Select((row => row.Split(','))).ToLookup(id => id[0], id => id[1]);

        idDictionary.ForEach(id => WriteLog("Input Id = {0} - Matching record Id = {1}", id.Key));

       var processList = data.Where(item => idDictionary.Contains(keyFilter(item.DataItem))).ToList();

        processList.ForEach(item => item.Id = idDictionary[keyFilter(item.DataItem)]);

        return processList;

DataItem的声明来自这个类:

public class ImportItem<T> : INotifyPropertyChanged
{
    public IEnumerable<string> DataItem
    {
        get; set; 

    }

    private bool visible;
    public bool Visible
    {
        get { return visible; }
        set
        {
            visible = value;
            RaisePropertyChanged("Visible");
        }
    }

    public string ErrorMessage { get; set; }

    public IEnumerable<string> Id
    {
        get; set;
    }

    public Status Status { get; set; }

    public ImportItem(T item)
    {
        DataItem = (IEnumerable<string>) item;
        visible = true;
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

1 个答案:

答案 0 :(得分:2)

什么是keyFilter?我猜它会返回IEnumerable,这意味着它没有包含Dictionary的Contains()重载。您可能需要以下内容:

var processList = 
data.Where(item => keyFilter(item.DataItem).Any(key => idDictionary.ContainsKey(key))).ToList()