C# 3.0 Lambda铸造误差
我正在使用lambda表达式,并尝试在添加到Hashset时转换为uint。下面是我正在做的:C# 3.0 Lambda铸造误差,c#-3.0,C# 3.0,我正在使用lambda表达式,并尝试在添加到Hashset时转换为uint。下面是我正在做的: HashSet<uint> LeadsInSession = new HashSet<uint>(); if (HttpContext.Current.Session["Category_SelectedLeadIds"] != null) { Dictionary<LeadPurchase.CategoryLeadSearch, List<stri
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.
}
有什么办法吗?你试过了吗
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);
}
您的问题是toRemoveLoasIOs是一个值类型为List的字典。所以toAdd是可数的,所以lead是一个列表,它相当合理地不会转换为uint
您需要在toAdd和(内部循环)上迭代,然后在lead上迭代各个字符串。比如:
foreach (var lead in toAdd) {
foreach (string value in lead) {
uint u;
if (UInt32.TryParse(value, out u)) {
LeadsInSession.Add(u);
}
}
}
对是我干的。但它不起作用,因为我的HashSet只接受uint&lead是List。可能正如另一个答案中所建议的。。您需要将hashset声明为HashSet@Richard:toRemoveLeadsIDs是值类型为List而不是List的字典。和HashSet仅接受uint。@user659469:oops,已更正。基本问题仍然存在,您需要在内部集合上循环,以获得要转换为数字的单个字符串。@Richard:如何在toAdd&internal循环上迭代?你能给我举个例子吗?先把列表展平会更好。@Jeff在这种情况下,我认为SelectMany只会增加混乱。