开发者

Lambda casting error

开发者 https://www.devze.com 2023-03-12 06:17 出处:网络
I\'m using lambda expression and trying convert into uint while adding into the Hashset. Here what I\'m doing:

I'm using lambda expression and trying convert into uint while adding into the Hashset. Here what I'm doing:

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.

  }

Any way around?


Your problem is that toRemoveLoasIOs is a dictionary with a value type of List<string>. Therefore toAdd will be IEnumerable<List<string>>, and thus lead is List<string> which, quite reasonably will not convert to uint.

You need to iterate over both toAdd and, an inner loop, over lead and then you have the individual strings to convert. Something like:

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


Did you Try ?

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);
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消