Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Tuesday, October 1, 2013

LINQ - SQL: validate null parameter and apply in where clause

I had the query

 var results =
                        from cs in skydeskDB.Cases
                        where
                        cs.AgentId == AgentId
                        && cs.ClosedAt >= iniDate
                        && cs.ClosedAt < endDate                        
                        select cs;

I wanted to avoid the agentid filter when the agentid filter were null, like

                  var results =
                        from cs in skydeskDB.Cases
                        where                        
                        cs.ClosedAt >= iniDate
                        && cs.ClosedAt < endDate                        
                        select cs;

and, when  agentid filter were different from null apply the agentid filter, like

var results =
                        from cs in skydeskDB.Cases
                        where
                        cs.AgentId == AgentId
                        && cs.ClosedAt >= iniDate
                        && cs.ClosedAt < endDate                        
                        select cs;

The solution is adding the condition (AgentId == null || cs.AgentId == AgentId):

var results =
                        from cs in skydeskDB.Cases
                        where
                        (AgentId == null || cs.AgentId == AgentId)
                        && cs.ClosedAt >= iniDate
                        && cs.ClosedAt < endDate                        

                        select cs;

now I can do both things with one single query:
 - avoid the agentid filter when the agentid filter were null
 - apply agentid filter when the agentid filter were different from null

Resource:
http://stackoverflow.com/questions/9505189/dynamically-generate-linq-queries

Tuesday, September 24, 2013

LINQ: LINQ to Entities no reconoce el método y este método no se puede traducir en una expresión de almacén

I was getting the error:

"LINQ to Entities no reconoce el método 'System.DateTime AddHours(Double)'
del método, y este método no se puede traducir en una expresión de almacén."

the code was:
   var results =
       from c in skydeskDB.Cases
       join cs in skydeskDB.CaseStates on c.CaseStateId equals cs.Id
      where
         c.HelpDeskId == new Guid("ee98652e-9fdf-435e-b325-74e7189b6561")
         && cs.Name != "closed"
         && c.EstimatedDate <= DateTime.Now
         && c.EstimatedDate >= DateTime.Now.AddHours(-1.0) // here was the problem
      group cs by cs.Name into g
      select new
      {
         type = "toexpire",
         statuscounter = g.Count(), 
         statusname = g.Key
       };

The solution was defining the DateTime.Now.AddHours(-1.0) outside the query:

       var estimatedDate = DateTime.Now.AddHours(-1); // here,  outside the query
       var results =
                           from c in skydeskDB.Cases
                            join cs in skydeskDB.CaseStates on c.CaseStateId equals cs.Id
                            where
                                c.HelpDeskId == new Guid("ee98652e-9fdf-435e-b325-74e7189b6561")
                                && cs.Name != "closed"
                                && c.EstimatedDate <= DateTime.Now
                                && c.EstimatedDate >= estimatedDate // here is already calculated
                            group cs by cs.Name into g
                            select new
                            {
                                type = "toexpire",
                                statuscounter = g.Count(), 
                                statusname = g.Key
                            };



Wednesday, July 3, 2013

LINQ: filter by two fields concatenation

When you hava an entiy with FirstName and LastName columns, and you need to filter by fullname, a quick solution is to filter by the fields value concatenation:

var agents = from a in DB.Agents
                                 where a.FirstName.Contains(name) ||
                                       a.LastName.Contains(name) ||
                                       String.Concat(a.FirstName, " ", a.LastName).Contains(name)
                                 select a;