tags:

views:

180

answers:

4

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

+8  A: 

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();
}
Jon Skeet
You mean >= startTime ;)
JustLoren
Ok I hoped it's possible to do with complex where clauses but I do it so now. thx.
snarebold
but how to handle it if i give both parameters?
snarebold
@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
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
Strings are always nullable - they're reference types.
Jon Skeet
+2  A: 

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

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.

snarebold
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
+1  A: 

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;
    }
daxsorbito
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
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