convert from List<value> to IEqualityComparer<strin

本文关键字:lt to IEqualityComparer gt strin value from convert List | 更新日期: 2023-09-27 18:05:07

我有Dictionary<string, List<Value>> FormControllerNamesValues属性在我的POCO对象中,这个字典将有所有下拉控制器名称和下拉控制器名称的选项列表。我的问题我不能我有这个编译错误

错误4参数3:不能从"System.Collections.Generic.List<Model.Value&gt"System.Collections.Generic.IEqualityComparer><string&gt">

我只是想问我如何修复这个错误

List<Value> dropDownListrValue =
    (from val in db.Values
     where val.ParentId == (from va in db.Values
                            where va.ParentId == (from value3 in db.Values
                                                  where value3.Name == formType
                                                  select value3.RecordId).FirstOrDefault()
                            select va.RecordId).FirstOrDefault()
    select val).ToList();
result = (from value1 in db.Values
          where value1.Name == formType
          select
              new ItemManagement
              {
                  FormType = value1.Name,
                  RecordID = value1.RecordId,     
                  FormControllerNames = 
                      (from va in db.Values 
                       where va.ParentId == (from value3 in db.Values where value3.Name ==formType select value3.RecordId).FirstOrDefault()
                       select va).ToDictionary(va => va.Name, dropDownListrValue)
              }).ToList();

这是我的ItemManagement课程:

public class ItemManagement
{
    public long RecordID { get; set; }
    public String FormType { get; set; }
    public Dictionary<string, List<Value>> FormControllerNamesValues { get; set; }
}

convert from List<value> to IEqualityComparer<strin

.ToDictionary(va => va.Name, dropDownListrValue)方法调用包含无效参数导致的错误。编译器将此调用解析为

public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    IEqualityComparer<TKey> comparer
)

重载,并且由于dropDownListrValue不实现IEqualityComparer接口,它抛出编译错误。

您可以更改对.ToDictionary(va => va.Name, va => dropDownListrValue)的调用来修复编译错误。这个方法调用将解析到

public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    Func<TSource, TElement> elementSelector
)

重载,并返回一个字典,该字典将包含每个va.Name键的dropDownListrValue值,假设这是您想要实现的。