user3004110
user3004110

Reputation: 969

How to dynamically change property in lambda expression

I have written a function below. In second line of code I have written "Where(t => t.PickTicket == EmployeeId)". I want to change (t.PickTicket) property dynamically. How can I achieve this??? And how can I make below code more better and shorter?

Thanks in advance.

public List<TicketJoin> GetTicketJoin(string EmployeeId)
        {
            var query = _context.tickets.Where(t => t.PickTicket == EmployeeId).Join
                     (
                     _context.employees, t => t.CreatedBy, e => e.EmployeeId, (t, e) => new
                     {
                         t.Id,
                         e.Employee_Name,
                         e.Department,
                         e.Location,
                         t.Subject,
                         t.MainCatId,
                         t.SubCatId,
                         t.PickTicket,
                         t.Status,
                         t.CreateDate
                     }).Join
                     (
                     _context.MainCategory, t => t.MainCatId, m => m.MainCatId, (t, m) => new
                     {
                         t.Id,
                         t.Employee_Name,
                         t.Department,
                         t.Location,
                         t.Subject,
                         t.MainCatId,
                         m.MainCatName,
                         t.SubCatId,
                         t.PickTicket,
                         t.Status,
                         t.CreateDate
                     }).Join
             (
             _context.SubCategory, t => t.SubCatId, s => s.SubCatId, (t, s) => new
             {
                 t.Id,
                 t.Employee_Name,
                 t.Department,
                 t.Location,
                 t.Subject,
                 t.MainCatId,
                 t.MainCatName,
                 t.SubCatId,
                 s.SubCatName,
                 t.PickTicket,
                 t.Status,
                 t.CreateDate
             }).ToList();

            var TicketJoin = query.ToList().Select(r => new TicketJoin 
            {
                EmployeeName = r.Employee_Name, 
                Subject = r.Subject, 
                Location = r.Location, 
                MainCatName = r.MainCatName, 
                SubCatName = r.SubCatName, 
                CreateDate = r.CreateDate
            }).ToList();

            return TicketJoin;
        }
    }

Upvotes: 2

Views: 335

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364299

How can I make it shorter?

By using database view to hide all those joins. In such case you will have have only simple query with single condition.

How can I make it dynamic?

Do you really need it dynamic? You can just do this:

public List<TicketJoin> GetTicketJoin(Expression<Func<Ticket, bool>> condition)
{
    var query = _context.Tickets.Where(condition)...  
}

and call it

var result = this.GetTicketJoin(t => t.PickTicket == employeeId);

It is not fully dynamic but it allows calling code specifying the condition.

Fully dynamic solution where you would be able to pass the property name as string requires using Expression trees to build query. There also used to be Dynamic-Linq library for this purpose.

Upvotes: 1

Related Questions