我需要根据复选框列表选择特定列

时间:2012-06-19 07:10:04

标签: c# asp.net-mvc linq entity-framework

我有一个复选框列表,用户可以选中或取消选中复选框。

根据选中的复选框,我用以逗号分隔的方式存储该值。现在问题是基于我需要单独获取该特定列的选中复选框。在“选择”**

db.Tasks.OrderBy(t => t.CreatedDate).ToList()
  .Select(t => new {
       Id = t.Id, 
       PriorityId = t.ProjectId, 
       Priority = t.Priority, 
       StatusId = t.StatusId, 
       Status = t.Status,
       EstimatedTime = t.EstimatedTime, 
       ActualTime = t.ActualTime, 
       Subject = t.Subject, 
       FileName = t.FileName,
       AssignedTo = t.AssignedTo, 
       Project = t.Project 
   }).ToList();

enter image description here

如果我选中复选框列表ActualTime,Subject,它应该是

db.Tasks.OrderBy(t => t.CreatedDate).ToList()
  .Select(t => new {
       Id = t.Id,       
       ActualTime = t.ActualTime, 
       Subject = t.Subject
   }).ToList();

如果我选中复选框列表Subject,FileName,AssignedTo,它应该是

db.Tasks.OrderBy(t => t.CreatedDate).ToList()
  .Select(t => new {
           Id = t.Id,  
           Subject = t.Subject, 
           FileName = t.FileName,
           AssignedTo = t.AssignedTo
       }).ToList();

根据选中的复选框列表,选择将是动态的。

1 个答案:

答案 0 :(得分:2)

将DynamicLibrary.cs添加到您的项目中。  你可以从link获得它。这是一个包含动态链接源的zip文件。这不是一个骗局。  最初发布在ScottGu的博客here上。  供参考,请参阅此堆栈溢出link

    using System.Linq.Dynamic;

    public class DynamicColumns : BaseEntity
    {
        public string User { get; set; }
        public string TaskId { get; set; }
        public string Project { get; set; }
        public string Priority { get; set; }
        public string TaskType { get; set; }
        public string Version { get; set; }
        public string Module { get; set; }
        public string Subject { get; set; }
        public string Details { get; set; }
        public string FileName { get; set; }
        public string Status { get; set; }          
        public string AssignedBy { get; set; }
        public string AssignedTo { get; set; }
        public int ActualTime { get; set; }
        public int LogWork { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime AssignedDate { get; set; }
        public DateTime ResolveDate { get; set; }
        public int EstimatedTime { get; set; }
    }

    public enum EnumTasks
    {

        User = 1,
        Project = 2,
        Priority = 3,
        TaskType = 4,
        Version = 5,
        Module = 6,
        Subject = 7,
        Details = 8,          
        Status = 9,            
        Assigned_By = 10,
        Assigned_To = 11,
        Created_Date = 12,
        Assigned_Date = 13,
        Resolve_Date = 14,
        Estimated_Time = 15,
        Actual_Time = 16,
        LogWork = 17
    }

    public IQueryable DynamicSelectionColumns()
    {
        using (var db = new TrackerDataContext())
        {
            string fieldIds = "," + "4,5,3,2,6,17,11,12" + ",";

            var taskColum = Enum.GetValues(typeof(EnumTasks)).Cast<EnumTasks>().Where(e => fieldIds.Contains("," + ((int)e).ToString() + ",")).Select(e => e.ToString().Replace("_", ""));

            string select = "new (  TaskId, " + (taskColum.Count() > 0 ? string.Join(", ", taskColum) + ", " : "") + "Id )";

            return db.Task.ToList().Select(t => new DynamicColumns() { Id = t.Id, TaskId = Project != null ? Project.Alias + "-" + t.Id : t.Id.ToString(), ActualTime = t.ActualTime, AssignedBy = t.AssignedBy.ToString(), AssignedDate = t.AssignedDate, AssignedTo = t.AssignedTo.ToString(), CreatedDate = t.CreatedDate, Details = t.Details, EstimatedTime = t.EstimatedTime, FileName = t.FileName, LogWork = t.LogWork, Module = t.Module != null ? t.Module.Name : "", Priority = t.Priority != null ? t.Priority.Name : "", Project = t.Project != null ? t.Project.Name : "", ResolveDate = t.ResolveDate, Status = t.Status != null ? t.Status.Name : "", Subject = t.Subject, TaskType = t.TaskType != null ? t.TaskType.Type : "", Version = t.Version != null ? t.Version.Name : "" }).ToList().AsQueryable().Select(select);
        }
    }