Linq query with parameter which can be empty?

GoTime ) { IQueryable query = DB. TABLENAME; if (startTime! = null) { query = query.

Where(i => i. StartTime >= startTime. Value); } if (goTime!

= null) { query = query. Where(i => i. GoTime == goTime.

Value); } return query.ToList(); }.

You mean >= startTime ;) – JustLoren Oct 26 '09 at 13:03 Ok I hoped it's possible to do with complex where clauses but I do it so now. Thx. – snarebold Oct 26 '09 at 13:13 but how to handle it if I give both parameters?

– snarebold Oct 26 '09 at 13:16 @JustLoren: Fixed, thanks. @snarebold: It will work just fine with both parameters - if you specify two "where" clauses, it'll act as a logical "AND". – Jon Skeet Oct 26 '09 at 13:41 I can't make nullable string.My parameters are strings and I have to conver it.

Because they come from webservice and you input the date there by a textbox – snarebold Oct 26 '09 at 13:48.

Try this "hacked" where clause: where (i. StartTime >= (startTime? I.

StartTime)) && (i. GoTime >= (goTime? I.

GoTime)) For this to work, startTime and goTime should be Nullable (or DateTime? ). This will work in all scenarios, i.e.When... Either of the parameters are null Both are null Both are not null.

This is a pretty good solution but this cancels out the item in the result if the goTime is null (in the database) and passing a null value in goTime parameter (with a non-null startTime). – daxsorbito Oct 26 '09 at 14:11 You are right. But as he did not mentioned that the fields are nullable in the database, I assumed they are not.

:) – Yogesh Oct 26 '09 at 15:02.

You could try something like this. Public static List GetAll(DateTime? StartTime, DateTime?

GoTime) { List getResultBetween = (from I in DB. TableName where (startTime. HasValue && i.

StartTime >= startTime) || (goTime. HasValue && i. GoTime >= goTime) select i).ToList(); return getResultBetween; }.

This will fail if both the values are not null. He did'nt mentioned that he needs both parameters in the question, but as a comment to Jon's answer. – Yogesh Oct 26 '09 at 13:43 You're right, I missed that one.

I think you could be played around modifying the where clause, adding restriction if both are null. – daxsorbito Oct 26 '09 at 14:06.

Strange it doesn't work. For more information. It's a webservice but I hope this is not the problem.My method looks like.

Public List AlleGeschaefteZwischenBestimmtemDatum(string StartDatum, string EndDatum) { IQueryable query = DB. FDPGeschaefte; if (StartDatum! = null && EndDatum!

= null) { query = query. Where(i => i. SysCreated > DateTime.

Parse(StartDatum) && i. SysCreated i. SysCreated >= DateTime.

Parse(StartDatum)); } if (EndDatum! = null) { query = query. Where(i => i.

SysCreated It throws an invalid datetime argument.

I suspect your strings are in the wrong format then - or at least that the parsing isn't propagated appropriately. I suggest you parse them before you use them in the query. – Jon Skeet Oct 26 '09 at 13:43.

IQueryable query = DB. = null && EndDatum! Query = query.

Where(i => i. SysCreated > DateTime.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions