-1

I want to declare empty variable for this condition,

 if(........)
 {
  emptyIQueryable= DBContext.MyTables.Join(MyTableTwos , x => x.ID , i => i.TID ,      ((x,i)  => 
  new{
      x,i
     }
  )).Where(x => x.ID == 1).SingleOrDefault();
  }
  else
  {
   emptyIQueryable= DBContext.MyTables.Join(MyTableTwos , x => x.ID , i => i.TID , ((x,i)  => 
  new{
     x,i
     }
  )).Where(x => x.ID == 2).SingleOrDefault();
  }

How can I declare it ?

4
  • It's unclear what you are trying to achieve. Try rephrasing your question. Jun 28, 2013 at 11:16
  • MyTable emptyIQueryable; should compile (with bad naming), the result of SingleOrDefault is not an IQueryable of any kind. Jun 28, 2013 at 11:19
  • @zey what do you mean "it cannot be"? Jun 28, 2013 at 11:20
  • @ErenErsönmez I think he means it won't compile :) Jun 28, 2013 at 11:21

2 Answers 2

3

SingleOrDefault does not return an IQueryable - it returns the object that MyTables consists of, or null. So if MyTables contains several objects of type say MyTable, that is the type for your variable:

MyTable result;
if(........)
{
    result = DBContext.MyTables.SingleOrDefault(x => x.ID == 1);
}
else
{
    result = DBContext.MyTables.SingleOrDefault(x => x.ID == 2);
}

Update. After edit thing get more complicated. The expression you are using returns object of type IEnumerable<anonymous>, which is impossible to state in code explicitly. You can, however, use either object or IEnumerable for emptyIQueryable, but you will loose some info about the object either way.

However this code can be easily refactored to get rid of the problem completely:

Func<MyTable, bool> condition;
if (.....)
{
    condition = x => x.ID == 1;
}
else
{
    condition = x => x.ID == 2;
}

var emptyIQueryable =
         DBContext.MyTables
                  .Join(MyTableTwos, x => x.ID, i => i.TID, ((x,i) => new {x,i}))
                  .Where(condition).SingleOrDefault();
2
  • +1 and Thanks Andrei , I've edit my question . How can I do it if I joined Two tables ?
    – zey
    Jun 28, 2013 at 11:28
  • @Andrei: It's actually not impossible to declare a variable as an anonymous type. See my answer. I don't recommend using that technique, but it is possible.
    – recursive
    Sep 23, 2014 at 1:02
0

It is possible to declare a variable as being an anonymous type without assigning to it. It's kind of a trick of the compiler, but you can do it like this.

var emptyIQueryable = false 
    ? new { x = default(MyTable), i = default(MyTableTwo) } 
    : null;

emptyIQueryable is assigned to null, and that anonymous object is never constructed. But it has the exact some IL type as you create later. The compiler guarantees that any two anonymously typed objects created in the same method that have the same properties with the same types in the same order will have the same type. Therefore, you could assign to it in your if block later.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.