Need day of week as string (Sunday, Monday ...) in Queryable where clause

c# dynamic-linq entity-framework-6 sql-server

Question

I need to check whether the provided keyword is present in the day-name of a date in the db. I have done it by using dynamic query.

ArrayList paramList = new ArrayList();
int paramCount = 0;
StringBuilder queryString = new StringBuilder();

if (keyword != null)
{
    queryString.Append(" and ClosureDate.Value.DayOfWeek.ToString().ToUpper().Contains(@" + paramCount + ")");
    paramList.Add(this.Keyword.ToUpper());
    paramCount++;
}
var abc= _context.ABC.Where(t => t.RecordStatus == 1).Select(t => t).OrderByDescending(t => t.Created).AsQueryable();
abc= abc.AsQueryable().Where(queryString[0].ToString(), searchArgs.ToArray());

This ToString() here is creating problem. I tried in other ways but, no luck. Can anyone help on it?

1
0
2/19/2020 2:00:39 PM

Popular Answer

You can write your SQL Query as below.

    DECLARE @Dates TABLE
(
  ProductID INT IDENTITY(1,1), 
  Dates DATETIME
)

INSERT INTO @Dates (Dates)
VALUES (GETDATE())
      ,(GETDATE()-1)
      ,(GETDATE()-2)
      ,(GETDATE()-3)
      ,(GETDATE()-4)
      ,(GETDATE()-5)

SELECT Dates 
FROM @Dates

SELECT Dates,DATENAME(WEEKDAY,Dates) AS DayOfWeek
FROM @Dates

Then in C#/MVC/ASP.Net App use the something like .Where(t => t.DayOfWeek.Contains("Monday")) to filter the result set.

0
2/19/2020 2:23:31 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow