2012-06-19 155 views
6

我有一個複選框列表,該用戶可以選中或取消選中該複選框。我需要根據複選框列表選擇特定列

根據選中的複選框,我用逗號分隔存儲該值。現在問題是基於選中的複選框,我需要單獨獲取該特定列。在 「選擇」 **

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,主題,它應該像

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

,如果我選擇複選框列表標題,文件名,AssignedTo,它應該像

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

選擇將基於選定的ch動態eck框列表。

+2

問題是?更多地解釋你的代碼。代碼中的複選框是如何表示的是「任務」?如果您顯示輸入和預期輸出,這也會更容易。 –

+0

因此,這些複選框用於使用戶選擇他想要選擇的列? –

+3

你尋求的答案可能在這裏http://stackoverflow.com/questions/606104/how-to-create-linq-expression-tree-with-anonymous-type-in​​-it –

回答

2

將DynamicLibrary.cs添加到您的項目中。 你可以從這個link得到它。這是一個包含動態鏈接源的zip文件。這不是一個DLL。 最初發布在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); 
     } 
    } 
+0

在動態LINQ查詢中外鍵對象無法正常工作。以便我創建一個模型並將該值分配給該模型。錯誤是連接已過期。 – Thulasiram

+0

感謝您的博客鏈接,花了一段時間尋找一個動態linq問題的解決方案,有無數奇怪的解決方案的一些答案,然後我發現顧5年前就這麼清楚了! –

+0

@ThulasiRam,你介意看看我的問題嗎?我正在嘗試實現動態LINQ查詢,但在檢查Scott和您的示例時遇到了一些麻煩。謝謝! https://stackoverflow.com/questions/28903387/implementing-dynamic-linq-querying-in-mvc5-ef-application –