0
public static SelectList IndicationsGroup(int? entityId, int? projectType, int? oldProjectType)
        {
            List<SelectListItem> oldSelectList = new List<SelectListItem>();
            List<SelectListItem> newSelectList = new List<SelectListItem>();

            ISpWeb_ProjectListResultSet newList = Chatham.Web.Proxies.TransactionsDataTier.ExecSpWeb_ProjectList(entityId, projectType);
            ISpWeb_ProjectListResultSet oldList = Chatham.Web.Proxies.TransactionsDataTier.ExecSpWeb_ProjectList(entityId, oldProjectType);

            foreach (SpWeb_ProjectList1LightDataObject item in newList.SpWeb_ProjectList1)
            {
                newSelectList.Add(new SelectListItem() { Text = item.ProjectName, Value = item.ProjectName.GetHashCode().ToString() });
            }
            foreach (SpWeb_ProjectList1LightDataObject item in oldList.SpWeb_ProjectList1)
            {
                oldSelectList.Add(new SelectListItem() { Text = item.ProjectName, Value = item.ProjectName.GetHashCode().ToString() });
            }

            // I want to return a union of the two select lists...
            // return new SelectList(unionedList, "Value", "Text");

        }

public class SelectListItem
    {
        public SelectListItem();

        public bool Selected { get; set; }
        public string Text { get; set; }
        public string Value { get; set; }
    }

How would I make my SelectListItem class IEquitable?

The code above should explain what I want to do in the comment. Someone told me I could use LINQ but I have no idea what I'm doing when it comes to LINQ. Thanks!

slandau
  • 23,528
  • 42
  • 122
  • 184

3 Answers3

3

You mean like this?

return new SelectList(oldSelectList.Union(newSelectList), "Value", "Text");
Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • I had NO idea that existed. I must have glossed over it the first time in Intellisense. DOH! – slandau Feb 21 '11 at 17:05
  • It's possible it won't produce the results you are expecting as it is using `object.Equals()` to determine the union set. If that is the case, there is an overload that takes an `IEqualityComparer` that lets you deal with that. – Matt Greer Feb 21 '11 at 17:07
3
// This will return a "union ALL" of the two select lists...
return new SelectList(newSelectList.Concat(oldSelectList), "Value", "Text");

//this will return a union of distinct values of the two selects,
//PROVIDED that SelectListItem is IEquatable
return new SelectList(newSelectList.Union(oldSelectList), "Value", "Text");

//this will return a union of distinct values of the two selects,
//given an implementation of an IEqualityComparer<SelectListItem> equalityComparer
//that will semantically compare two SelectListItems
return new SelectList(newSelectList.Union(oldSelectList, equalityComparer), "Value", "Text");
KeithS
  • 70,210
  • 21
  • 112
  • 164
  • I'd prefer to use the middle one and make my SelectListItem class IEquitable. What would the GetHashCode function look like though? – slandau Feb 21 '11 at 17:50
  • Check this out: http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode. It's simple and unlikely to generate collisions (but understand that no one-way hash that digests a message into a smaller bit size can guarantee no collisions). – KeithS Feb 21 '11 at 17:57
0
return new SelectList(newSelectList.Concat(newSelectList), "Value", "Text");
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928