Lambda铸造错误

时间:2011-06-10 17:12:54

标签: c#-3.0

我正在使用lambda表达式并尝试在添加到Hashset时转换为uint。我正在做什么:

HashSet<uint> LeadsInSession = new HashSet<uint>();


if (HttpContext.Current.Session["Category_SelectedLeadIds"] != null)
  {
    Dictionary<LeadPurchase.CategoryLeadSearch, List<string>> toRemoveLeadsIDs =
      (Dictionary<LeadPurchase.CategoryLeadSearch, List<string>>)HttpContext.Current.Session["Category_SelectedLeadIds"];

    LeadPurchase.CategoryLeadSearch searches = (LeadPurchase.CategoryLeadSearch)HttpContext.Current.Session["searches"];

    var toAdd = toRemoveLeadsIDs.Where(Pair => Pair.Key.VerticalID == searches.VerticalID)
                                   .Select(Pair => Pair.Value)
                                   .ToList();

    foreach (var lead in toAdd)
      LeadsInSession.Add(lead);// I need to convert lead into uint. Convert.ToUInt32() didn't work here.

  }

任何方式?

2 个答案:

答案 0 :(得分:0)

你试过吗?

LeadPurchase.CategoryLeadSearch searches
   = (LeadPurchase.CategoryLeadSearch) HttpContext.Current.Session["searches"];
var toAdd = toRemoveLeadsIDs.Where(Pair => Pair.Key.VerticalID == searches.VerticalID)
                            .Select(Pair => (uint)Pair.Value)
                            .ToList<uint>();
foreach (var lead in toAdd)
  LeadsInSession.Add(lead);
} 

答案 1 :(得分:0)

您的问题是toRemoveLoasIOs是一个值类型为List<string>的字典。因此,toAdd将为IEnumerable<List<string>>,因此leadList<string>,相当合理地不会转换为uint

您需要遍历toAdd和内部循环,再遍历lead,然后您需要转换单个string。类似的东西:

foreach (var lead in toAdd) {
  foreach (string value in lead) {
    uint u;
    if (UInt32.TryParse(value, out u)) {
      LeadsInSession.Add(u);
    }
  }
}