Advertisement
altair0010

FindRated

Feb 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. private long FindRated(FormCollection form)
  2. {
  3. double weightThreshold = double.Parse(ConfigurationManager.AppSettings["contactThreshold"]);
  4. using (var db = new Entities())
  5. {
  6. List<string> contactValuesList = (from ric in _ratingInitialContacts
  7. where !String.IsNullOrEmpty(form["contact_" + ric.Key])
  8. select form["contact_" + ric.Key]).ToList();
  9.  
  10. contactValuesList.AddRange(from string key in form.Keys
  11. where key.StartsWith("os_site")
  12. select int.Parse(key.Substring(("os_site").Length))
  13. into cn
  14. where !String.IsNullOrEmpty(form["os_username" + cn])
  15. select form["os_username" + cn]);
  16.  
  17. var matchingContacts = db.rated_contact.Where(x => contactValuesList.Contains(x.value));
  18. if (matchingContacts.Any())
  19. {
  20. if (matchingContacts.Count() > 1)
  21. {
  22. //Compute weight to choose...
  23. var matchedThreshold = matchingContacts.Where(w => w.contact_type.weight >= weightThreshold);
  24. if (matchedThreshold.Any())
  25. {
  26. if (matchedThreshold.Count() > 1)
  27. {
  28. //multiple matching contacts, attempt to merge...
  29. return CreateMerge(from r in db.rateds
  30. join rc in matchedThreshold
  31. on r.ratedId equals rc.ratedId
  32. select r) * -1;
  33. }
  34. else
  35. {
  36. return matchedThreshold.First().ratedId;
  37. }
  38. }
  39. else
  40. {
  41. //none matched the threshold, attempt to merge...
  42. return CreateMerge(from r in db.rateds
  43. join rc in matchingContacts
  44. on r.ratedId equals rc.ratedId
  45. select r) * -1;
  46. }
  47. }
  48. else
  49. {
  50. //Only one matched, return.
  51. return matchingContacts.First().ratedId;
  52. }
  53. }
  54. else
  55. {
  56. //Nothing matched, return 0 to create.
  57. return 0;
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement