tags:

views:

51

answers:

1

Hi,

I have written a query to get result from table.

var queryres = (from r in objTestEntities.module_where r.PID ==id select r).ToList();
try
{
for (int j = 0; j < queryres.Count; j++)
{
string sModuleName = queryres[j].Title;
int iModuleId = queryres[j].ModuleID;

string sVersion = queryres[j].VersionNo;
sModuleNameList.Add(new SelectListItem
{
Text = sModuleName,
Value = iModuleId.ToString(),
Selected = true
});



sVersionList.Add(new SelectListItem
{
Text = sVersion,
Value = sVersion,
Selected = true
});

}


ViewData["Module"] = sModuleNameList;
ViewData["Version"] = sVersionList;
}

Now I want that sVersionList which is of type should have distinct value. But as from the queryres variable I am getting module name, and different module can have same version.

From above code in version list combo I am getting same version no multiple times.

But I want that one version should be seen only one time.

I have tried this also: ViewData["Version"] = sVersionList.Distinct(); But from this also I am not able to show the distict value in version list combo.

+1  A: 
var moduleNameList = queryres.Select(o => new SelectListItem 
{ 
    Text = o.Title, Value = o.ModuleID.ToString(), Selected = true 
});
var versionList = queryres
    .Select(o => o.VersionNo)
    .Distinct()
    .Select(o => new SelectListItem 
    { 
        Text = o, Value = o, Selected = true 
    });
Kirk Woll
I thought there wasn't a default implementation of IEqualityComparer for string... Which is about as silly as there not being an implementation of ForEach() on IEnumerable..
Jimmy Hoffa