1

I have 2 important date fields in db.

startTime and goTime

I like to create custom query in which one parameter may be empty, see my example

public List<Type> GetAll( DateTime startTime, DateTime goTime )
{
List<Type> getResultBetween =

   (from i in DB.TABLENAME
    where i.startTime >= startTime && i.goTime == ANYTHING
    select i).ToList();
    return getResultBetween;
}

So goal is now that I get all up to the given startTime even the goTime is not defined. It should also work if I define the goTime and let the Starttime empty. Resault should give me all till the gotime.

Thank you

4 Answers 4

9

Try something like this, using nullable types and building up the query explicitly:

public List<Type> GetAll(DateTime? startTime, DateTime? goTime )
{
    IQueryable<Type> 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();
}
Sign up to request clarification or add additional context in comments.

5 Comments

Ok I hoped it's possible to do with complex where clauses but I do it so now. thx.
but how to handle it if i give both parameters?
@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".
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
Strings are always nullable - they're reference types.
2

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<DateTime> (or DateTime?).

This will work in all scenarios, i.e. when...

  1. Either of the parameters are null
  2. Both are null
  3. Both are not null

2 Comments

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).
You are right. But as he did not mentioned that the fields are nullable in the database, I assumed they are not. :)
0

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<FDPGeschaefte> AlleGeschaefteZwischenBestimmtemDatum(string StartDatum, string EndDatum)
{
    IQueryable<FDPGeschaefte> query = DB.FDPGeschaefte;
    if (StartDatum != null && EndDatum != null)
    {
        query = query.Where(i => i.SysCreated > DateTime.Parse(StartDatum) && i.SysCreated <= DateTime.Parse(EndDatum));
    }
    if (StartDatum != null)
    {
        query = query.Where(i => i.SysCreated >= DateTime.Parse(StartDatum));
    }
    if (EndDatum != null)
    {
        query = query.Where(i => i.SysCreated <= DateTime.Parse(EndDatum));
    }
    return query.ToList();
}

if i type just one parameter in the webservice. it throws an invalid datetime argument.

1 Comment

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.
0

Hi you could try something like this.

public static List<Type> GetAll(DateTime? startTime, DateTime? goTime)
    {
        List<Type> getResultBetween =
            (from i in DB.TableName
             where (startTime.HasValue && i.StartTime >= startTime)
                   || (goTime.HasValue && i.GoTime >= goTime)
             select i).ToList();
        return getResultBetween;
    }

2 Comments

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.
You're right, I missed that one. I think you could be played around modifying the where clause, adding restriction if both are null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.