1

I'm working with Simple.data, and the answer is not in the technology aforementioned but helps bring the point across. So ignore the syntax etc.

I am querying a database with a simple query; but based on a set of conditions, the query will change.

So for example: (very simplistic, probably 5-10 conditions)

     var result;

     if(LoggedAtSelected)
     {
      // Condition 1 - Calls Logged after a certain date
      result = db.Jobs.FindAll(db.Jobs.Logged_At >= startDate);
     }
     else 
     {
      // Condition 2 - Calls Closed after a certain date
      result = db.Jobs.FindAll(db.Jobs.Closed_At >= startDate && dd.Jobs.Closed_At <= endDate);
     }

     foreach(var JobRecord in result)
     {
     }

This is the ideal code above, but sadly this is not possible given the dynamic binding and variable nature of var. What is the best practice for this kind of situation? My only idea is to write a "var result = condition..." for every condition, and in the if..else if..else, to assign it to a global variable after converting it to that type; and then using it in the "foreach". Sounds a lot of work. Any ideas? Or is that it!!!?!!!

Dane Balia
  • 5,271
  • 5
  • 32
  • 57
  • 3
    What will the actual type of 'var' be? and use that? – RvdK Sep 03 '12 at 12:14
  • `var` doesn't make `result` dynamic, it's still statically typed. It just saves you writing out the type. As PoweRoy says, work out what type you want it to be, e.g. `IEnumerable` and make declare `result` to be that type. – Mark Pattison Sep 03 '12 at 12:16
  • @MarkPattison - `JobRecords` is the variable name for each item... – Oded Sep 03 '12 at 12:17
  • Sadly with Simple.Data the "type" doesn't exist. So I guess this comes down to a Simple.Data problem. I will research that, but indeed I can solve this in others areas where I know the type – Dane Balia Sep 03 '12 at 12:17
  • @Oded - sorry, misread the `foreach`. – Mark Pattison Sep 03 '12 at 12:19
  • 1
    @DaneBalia - The type may simply be `dynamic` in that case. – Oded Sep 03 '12 at 12:20

3 Answers3

6

Instead of:

var result;

Use the actual type returned by db.Jobs.FindAll:

IEnumerable<Job> result;
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Thanks Oded, just have to find out if Simple.Data can present a "static" type to use for IEnumerable (somewhere it should know this). But I have voted up the answer :) – Dane Balia Sep 03 '12 at 12:19
  • Yes, Simple.Data will automagically cast the result from FindAll to an IEnumerable as long as the property names in T roughly match the column names in the table. – Mark Rendle Sep 03 '12 at 13:05
0

You can only use var if the compiler can know exactly which type to use (or how to define a new type for you).

In your case you can either define it with the type say

List<Job> result;

or call the constructor to return an instance:

var result = new List<Job>;

(of course your query will return an IEnumarable instance instead of a List, I just used List as an example because you can't instantiate an enumeration.)

Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50
0

Just as a note, as your if statements determine the filters for the query rather than the query itself, you might want to build up a SimpleExpression there and run the query afterwards. For example.

var whereCLause;

if(LoggedAtSelected)
{
  // Condition 1 - Calls Logged after a certain date
  whereClause = db.Jobs.Logged_At >= startDate;
}
else 
{
  // Condition 2 - Calls Closed after a certain date
  whereClause = db.Jobs.Closed_At >= startDate && dd.Jobs.Closed_At <= endDate;
}

List<Job> results = db.Jobs.All.Where(whereClause);
foreach(Job record in results)
{
  ...
}
Dan Maharry
  • 1,499
  • 2
  • 19
  • 35