Expert wanna be Expert wanna be - 3 months ago 9
C# Question

C#, how to organize this code?

I have a method like below, which is to get the list after filter and sorting.

var sample = dbContext.sample;

// search
if (!String.IsNullOrEmpty(searchModel.name)){
sample = sample.Where( x=> x.name == searchModel.name);
}

if (!String.IsNullOrEmpty(searchModel.modelNo)){
sample = sample.Where( x=> x.modelNo == searchModel.modelNo);
}

if (!String.IsNullOrEmpty(searchModel.serialNo)){
sample = sample.Where( x=> x.serialNo == searchModel.serialNo);
}

if (!String.IsNullOrEmpty(searchModel.manufacture)){
sample = sample.Where( x=> x.manufacture == searchModel.manufacture);
}

if (!String.IsNullOrEmpty(searchModel.managerId)){
sample = sample.Where( x=> x.managerId == searchModel.managerId);
}

if (!String.IsNullOrEmpty(searchModel.location)){
sample = sample.Where( x=> x.location == searchModel.location);
}

// sort
if (searchModel.sortMethod == 'asc') {
switch (searchModel.sortField) {
case 'name':
sample = sample.OrderBy( x => x.name);
break;
case 'modelNo':
sample = sample.OrderBy( x => x.modelNo);
break;
case 'serialNo':
sample = sample.OrderBy( x => x.serialNo);
break;
case 'manufacture':
sample = sample.OrderBy( x => x.manufacture);
break;
case 'managerId':
sample = sample.OrderBy( x => x.managerId);
break;
case 'location':
sample = sample.OrderBy( x => x.location);
break;
default:
sample = sample.OrderBy( x => x.id);
break;
}
} else {
switch (searchModel.sortField) {
case 'name':
sample = sample.OrderByDescending( x => x.name);
break;
case 'modelNo':
sample = sample.OrderByDescending( x => x.modelNo);
break;
case 'serialNo':
sample = sample.OrderByDescending( x => x.serialNo);
break;
case 'manufacture':
sample = sample.OrderByDescending( x => x.manufacture);
break;
case 'managerId':
sample = sample.OrderByDescending( x => x.managerId);
break;
case 'location':
sample = sample.OrderByDescending( x => x.location);
break;
default:
sample = sample.OrderByDescending( x => x.id);
break;
}
}

return sample.ToList();


It's dirty and very long, So I organized with separate methods, like,

private DbSet<Sample> Filter(DbSet<sample> sample, SearchModel searchModel) {
// search
if (!String.IsNullOrEmpty(searchModel.name)){
sample = sample.Where( x=> x.name == searchModel.name);
}
//...
return sample;
}

private DbSet<Sample> Sorting(DbSet<sample> sample, SearchModel searchModel) {
// sort
if (searchModel.sortMethod == 'asc') {
switch (searchModel.sortField) {
case 'name':
sample = sample.OrderBy( x => x.name);
break;
return sample;
// ...

return sample;
}


Now it's a little organized, but still it's very long.

I don't think Pro will never do this way.

Please advice me how I can organize this codes nicely and clean.

Answer

Luckily you can achieve this by using reflection

sample = sample.OrderBy(x=> x.GetType().GetProperty(searchModel.sortField).GetValue(x, null));

sample = sample.OrderByDescending(x=> x.GetType().GetProperty(searchModel.sortField).GetValue(x, null));
Comments