c#最小化语句
本文关键字:语句 最小化 | 更新日期: 2023-09-27 18:06:28
如何最小化下面的代码:
if (sort == "none")
{
var notes = partial.OrderBy(x => x.Rank).OrderBy(x => x.Stat).OrderByDescending(x => x.Note_Strength_Value).Skip(startIndex).Take(BlockSize).ToList();
NoteList.Clear();
if (notes.Count() > 0)
{
foreach (var item in notes)
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"" + item.ID + "'" class='"trPregame'"><td style='"text-align:left; width:100%;'" id='"notesonly'">" + item.Note1 + "</td></tr>",
});
}
}
else
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"noresult'"><td colspan='"1'" style='"text-align:center'"><b>No Results Found!</b></td></tr>"
});
}
}
if (sorttype == "Note")
{
if (sort == "ascending")
{
IList<NoteModel2> partial3 = new List<NoteModel2>();
foreach (var i in partial)
{
partial3.Add(new NoteModel2
{
ID = i.ID,
Note1 = i.Note1,
Note2 = Regex.Replace(i.Note1.Trim(), @"<(.|'n)*?>", string.Empty),
Rank = (int)i.Rank
});
}
var notes = partial3.OrderBy(x => x.Note2).Skip(startIndex).Take(BlockSize).ToList();
NoteList.Clear();
if (notes.Count() > 0)
{
foreach (var item in notes)
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"" + item.ID + "'" class='"trPregame'"><td style='"text-align:left; width:100%;'" id='"notesonly'">" + item.Note1 + "</td></tr>",
});
}
}
else
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"noresult'"><td colspan='"1'" style='"text-align:center'"><b>No Results Found!</b></td></tr>"
});
}
}
if (sort == "descending")
{
IList<NoteModel2> partial3 = new List<NoteModel2>();
foreach (var i in partial)
{
partial3.Add(new NoteModel2
{
ID = i.ID,
Note1 = i.Note1,
Note2 = Regex.Replace(i.Note1.Trim(), @"<(.|'n)*?>", string.Empty),
Rank = (int)i.Rank
});
}
var notes = partial3.OrderByDescending(x => x.Note2).Skip(startIndex).Take(BlockSize).ToList();
NoteList.Clear();
if (notes.Count() > 0)
{
foreach (var item in notes)
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"" + item.ID + "'" class='"trPregame'"><td style='"text-align:left; width:100%;'" id='"notesonly'">" + item.Note1 + "</td></tr>",
});
}
}
else
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"noresult'"><td colspan='"1'" style='"text-align:center'"><b>No Results Found!</b></td></tr>"
});
}
}
}
//SORT BY RANK
if (sorttype == "Rank")
{
if (sort == "ascending")
{
IList<NoteModel2> partial3 = new List<NoteModel2>();
foreach (var i in partial)
{
partial3.Add(new NoteModel2
{
ID = i.ID,
Note1 = i.Note1,
Note2 = Regex.Replace(i.Note1.Trim(), @"<(.|'n)*?>", string.Empty),
Rank = (int)i.Rank
});
}
var notes = partial3.OrderBy(x => x.Rank).Skip(startIndex).Take(BlockSize).ToList();
NoteList.Clear();
if (notes.Count() > 0)
{
foreach (var item in notes)
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"" + item.ID + "'" class='"trPregame'"><td style='"text-align:left; width:100%;'" id='"notesonly'">" + item.Note1 + "</td></tr>",
});
}
}
else
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"noresult'"><td colspan='"1'" style='"text-align:center'"><b>No Results Found!</b></td></tr>"
});
}
}
if (sort == "descending")
{
IList<NoteModel2> partial3 = new List<NoteModel2>();
foreach (var i in partial)
{
partial3.Add(new NoteModel2
{
ID = i.ID,
Note1 = i.Note1,
Note2 = Regex.Replace(i.Note1.Trim(), @"<(.|'n)*?>", string.Empty),
Rank = (int)i.Rank
});
}
var notes = partial3.OrderByDescending(x => x.Rank).Skip(startIndex).Take(BlockSize).ToList();
NoteList.Clear();
if (notes.Count() > 0)
{
foreach (var item in notes)
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"" + item.ID + "'" class='"trPregame'"><td style='"text-align:left; width:100%;'" id='"notesonly'">" + item.Note1 + "</td></tr>",
});
}
}
else
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"noresult'"><td colspan='"1'" style='"text-align:center'"><b>No Results Found!</b></td></tr>"
});
}
}
}
是否有其他方法来最小化代码或优化它?
排序如下:
- none默认按音符强度值降序排序,按Stat和Rank升序排序。 提升
- 下行
和sorttypes:
- 注意
谢谢
- 将notes.Count()> 0替换为notes.Any()
-
替换
foreach (var item in notes) { NoteList.Add(new NoteModel { Note1 = "<tr id='"" + item.ID + "'" class='"trPregame'"><td style='"text-align:left; width:100%;'" id='"notesonly'">" + item.Note1 + "</td></tr>", }); }
NoteList.AddRange(notes.Select(item=> new NoteModel { Note1 = "<tr id='"" + item.ID + "'" class='"trPregame'"><td style='"text-align:left; width:100%;'" id='"notesonly'">" + item.Note1 + "</td></tr>"} ));
你用Resharper吗?这很有帮助!
我注意到,if (sort==…)中的语句是相同的,所以将其替换为:
IList<NoteModel2> partial3 = new List<NoteModel2>();
foreach (var i in partial)
{
partial3.Add(new NoteModel2
{
ID = i.ID,
Note1 = i.Note1,
Note2 = Regex.Replace(i.Note1.Trim(), @"<(.|'n)*?>", string.Empty),
Rank = (int)i.Rank
});
}
IList<NoteModel2> notes;
if (sort == "none")
{
notes = partial.OrderBy(x => x.Rank).OrderBy(x => x.Stat).OrderByDescending(x => x.Note_Strength_Value).Skip(startIndex).Take(BlockSize).ToList();
}
if (sort == "ascending")
{
notes = partial3.OrderBy(x => x.Note2).Skip(startIndex).Take(BlockSize).ToList();
}
else
{
notes = partial3.OrderByDescending(x => x.Note2).Skip(startIndex).Take(BlockSize).ToList();
}
NoteList.Clear();
if (notes.Count() > 0)
{
foreach (var item in notes)
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"" + item.ID + "'" class='"trPregame'"><td style='"text-align:left; width:100%;'" id='"notesonly'">" + item.Note1 + "</td></tr>",
});
}
}
else
{
NoteList.Add(new NoteModel
{
Note1 = "<tr id='"noresult'"><td colspan='"1'" style='"text-align:center'"><b>No Results Found!</b></td></tr>"
});
}