6

I have a check box list in that user can check or uncheck the check box.

Based on the selected check box I used to store that value by comma separated. Now the problem is based on selected check box I need to get that particular column alone. in "select"**

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

if i select in check box list ActualTime, Subject, it should be like

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

if i select in check box list Subject, FileName, AssignedTo, it should be like

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

the select will be dynamic based on selected check box list.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Thulasiram
  • 8,432
  • 8
  • 46
  • 54
  • 2
    And the question is? Explain your code more. How is the checkboxes represented in code, is that the `Tasks`? It would also be easier if you showed the input and expected output. – Tomas Jansson Jun 19 '12 at 07:15
  • So, these checkboxes are used to make the user select what columns he want to select??? – Mahmoud Gamal Jun 19 '12 at 08:01
  • 3
    The answer you seek might be here http://stackoverflow.com/questions/606104/how-to-create-linq-expression-tree-with-anonymous-type-in-it – Mike Miller Jun 19 '12 at 08:09
  • I am also confused about what the actual question is here. – Tom Bushell Jun 19 '12 at 18:10
  • @Tom Bushell: i had edited the question. now u can see it. – Thulasiram Jun 20 '12 at 06:08
  • 1
    Why not just get all of the columns and then hide/display the columns in your page as the checkboxes indicate? That would be a whole lot easier. – rikitikitik Jun 20 '12 at 06:26
  • i need to pass only the selected check box column alone using this Type modelObj = collection.FirstOrDefault().GetType(); PropertyInfo[] modelProperties = modelObj.GetProperties(); i used to show the particular column base on user selected in check box list. – Thulasiram Jun 20 '12 at 06:30
  • How is the link shared by Mike Miller not satisfying you? – Polity Jun 20 '12 at 07:47

1 Answers1

2

add DynamicLibrary.cs to your project. You can get it from this link . It's a zip file that contains the dynamic link source. It's not a dll. Originally posted on ScottGu's blog here. for reference see this stack overflow 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);
        }
    }
Community
  • 1
  • 1
Thulasiram
  • 8,432
  • 8
  • 46
  • 54
  • in dynamic linq query foreign key object is not working. so that i created one model and assigned that value to that model. error is connection is expired. – Thulasiram Jun 21 '12 at 15:02
  • Thanks for the Gu blog link, spent a while scouting around SO for some answers to a dynamic linq problem, with countless wierd solutions, then I find Gu made it all so clear 5 years ago ! – dave heywood Oct 17 '13 at 08:20
  • @ThulasiRam, would you mind taking a look at my question? I'm trying to implement Dynamic LINQ querying, but I'm having some trouble getting up and running while reviewing both Scott's and your examples. Thanks! https://stackoverflow.com/questions/28903387/implementing-dynamic-linq-querying-in-mvc5-ef-application – Analytic Lunatic Mar 09 '15 at 16:01