How to concatenate a date and a time string and compare with a DateTime using LINQ

288 Views Asked by At

I have two strings, one of them for time only and the other for the date. I compare the concatenation with a DateTime.

I want to translate this SQl query to LINQ:

select count(*) from ViewAllTranUser
where TypeForm=15 
and DateCreate +' '+ TimeCreate<='2022/11/06 02:49:51'
and NameButton=2)>0 

With:

DateCreate:2021/01/15
TimeCreate:02:38:57

I want to get the count of rows where DateTime is less than the concatenation of a given date and time.

1

There are 1 best solutions below

1
Mohamed Fouad On

the Problem was receive date and Time and in database the date and time not in the same Field so I found the Solution by make the received as Text such as that can replace the date - by / such as

var dateRelaced = actualQuantityManualDTO.DateGr.Replace("-", "/");

after that make text to collect the date and time not every one of them is seperated by making map

CreateMap<ViewAllTranUser, ViewAllTranUserDTO>() .ForMember(dest => dest.DateandTime, opt => opt.MapFrom(src => (src.DateCreate + ' ' + src.TimeCreate)));

and in the List Because The date be in arabic So I Make replace another for every item in list

var viewTransferList = _mapper.Map<List>(viewQuery);

    foreach (var date in viewTransferList) {
        date.DateandTime = date.DateandTime.Replace("ص", "AM");
        date.DateandTime = date.DateandTime.Replace("م", "PM");
    }

after that To make Comparison ca't make it in string type so I translate it

var MapperList = viewTransferList.Where(x => DateTime.Parse(x.DateandTime) <= DateTime.Parse(dateRelaced)).ToList();

at end Is successed