Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 使用select减少交换机中的代码重复?_Asp.net Mvc 4_Switch Statement_Redundancy - Fatal编程技术网

Asp.net mvc 4 使用select减少交换机中的代码重复?

Asp.net mvc 4 使用select减少交换机中的代码重复?,asp.net-mvc-4,switch-statement,redundancy,Asp.net Mvc 4,Switch Statement,Redundancy,我有这个选择开关 switch (sorting) { case "newest": { var userId = User.Identity.GetUserId(); var model = db.Posts.Include(p => p.Vote)

我有这个选择开关

switch (sorting)
            {
                case "newest":
                {
                        var userId = User.Identity.GetUserId();
                        var model = db.Posts.Include(p => p.Vote)
                            .OrderByDescending(p => p.PostId).ToList()
                            .Select(p => new ListPostsViewModel
                            {
                                UserVotedUpOnPost = p.Vote.Any(u => u.ApplicationUserID == userId && u.PostVote == 1),
                                UserVotedDownOnPost = p.Vote.Any(u => u.ApplicationUserID == userId && u.PostVote == -1),
                                PostId = p.PostId.ToString(),
                                TimeAgo = ToRelativeDate(p.MessageDate),
                                Message = p.Message,
                                TotalVotes = p.Vote.Sum(v => v.PostVote)
                            }).ToList();
                    return View("Index", model);

                } break;

                case "oldest":
                    {
                        var userId = User.Identity.GetUserId();
                        var model = db.Posts.Include(p => p.Vote)
                            .OrderBy(p => p.PostId).ToList()
                            .Select(p => new ListPostsViewModel
                            {
                                UserVotedUpOnPost = p.Vote.Any(u => u.ApplicationUserID == userId && u.PostVote == 1),
                                UserVotedDownOnPost = p.Vote.Any(u => u.ApplicationUserID == userId && u.PostVote == -1),
                                PostId = p.PostId.ToString(),
                                TimeAgo = ToRelativeDate(p.MessageDate),
                                Message = p.Message,
                                TotalVotes = p.Vote.Sum(v => v.PostVote)
                            }).ToList();
                        return View("Index", model);
                    }
                    break;
}
如您所见,唯一不同的行是
.OrderByDescending(p=>p.PostId).ToList()
,我将有一些类似的行,但如何编写它而不复制select等中的所有代码

类似于:

var qry = db.Posts.Include(p => p.Vote);
select (sorting) {
  case "newest":
    qry = qry.OrderBy(x => x.Date);
    break;

  case "oldest":
    qry = qry.OrderByDescending(x => x.Date);
    break;
}
var res = qry.Select(p => new ListPostsViewModel {
                       …
                     });
 return View("Index, res);

var data=db.Posts.Include(p=>p.Vote)然后使用switch语句对其进行排序-
case“newest”:data=data.OrderBy(…)
等,最后选择
var model=data.Select(…