我有一些我正在尝试重构的 linq 代码,因为它不是很好:
基本上,我想知道是否有更好的方法来执行以下操作:
if (!string.IsNullOrWhiteSpace(_filter.AssignedTo)
{
var query = from ticket in dataClassesDataContext.TicketsIssues
where ticket.ClosedDate == null
&& cUser.GetUserNameUsingGUID(ticket.AssignTicketToUser) == _filter.AssignedTo
select new
{
Priority = ticket.TicketPriority.TicketPriorityName,
Description = ticket.Description.Replace("\n", ", "),
};
}
else
{
var query = from ticket in dataClassesDataContext.TicketsIssues
where ticket.ClosedDate == null
select new
{
Priority = ticket.TicketPriority.TicketPriorityName,
Description = ticket.Description.Replace("\n", ", "),
};
}
除了 where 子句检查 AssignTicketToUser 之外,它们都是相同的。
我希望有更好的方法来避免使用 if else 语句?我有一些这样的代码块,不想大量重复代码!