tags:

views:

75

answers:

3

In the code below, I need to set "Variable1" to an arbitrary value so I don't get into scope issues further below. What is the best arbitrary value for a variable of type var, or is there a better way to avoid the scope issue I'm having?

var Variable1;
if(Something == 0)
{
    //DB = DatabaseObject
    Variable1 = 
        from a in DB.Table
        select new {Data = a};
}
int RowTotal = Variable1.Count();
+3  A: 

Well, you could do:

// It's not clear from your example what the type of Data should
// be; adjust accordingly.
var variable1 = Enumerable.Repeat(new { Data = 0 }, 0).AsQueryable();
if (something == 0)
{
    //DB = DatabaseObject
    variable1 = from a in DB.Table
                select new {Data = a};
}
int rowTotal = variable1.Count();

This is effectively "typing by example". To be honest, I'd try to avoid it - but it's hard to know exactly how I'd do so without seeing the rest of the method. If possible, I'd try to keep the anonymous type scope as tight as possible.

Note: in this case you could just select a instead of an anonymous type. I'm assuming your real use case is more complex. Likewise if you genuinely only need the row total, then set that inside the braces. The above solution is only applicable if you really, really need the value of the variable later on.

Jon Skeet
I'd hate to argue with the Skeet, but will that work? The Repeat will create an IEnumerable<anon type>, while the Linq will want an IQueryable<anon type>. Also, aren't all anonymous type distinct, even if identically defined?
James Curran
@James: In a word, no.
Steven Sudit
@James: Nope, anonymous types with the same property names and types specified in the same order are guaranteed to be reused. C# 4 spec, section 7.6.10.6: "Within the same program, two anonymous object initializers that specify a sequence of properties of the same names and compile-time types in the same order will produce instances of the same anonymous type."
Jon Skeet
@James: However, your point about `IQueryable` is well taken. Fixing. Although it would have worked anyway - `variable1` would have been of type `IEnumerable<T>`, and `IQueryable<T>` extends `IEnumerable<T>` anyway. But it's better to be `IQueryable<T>` so that anything else done with it (e.g. `Count()`) gets the chance to work at the database.
Jon Skeet
@Jon: Do you want to weigh in on the dynamic/object issue in the comments to the question?
Steven Sudit
A: 

It looks like you can define it as IEnumerable. Then you can use the count function like you are trying to.

spinon
That would lose the type safety.
Steven Sudit
@Mike: `IEnumerable` is a non-generic interface: it returns some numbers of `object`'s. To keep type safety, it would have to be `IEnumerable<T>`, where T is that ineffable anonymous type. Worse, given the `object`, you can't downcast it to the anon type.
Steven Sudit
Thanks, I realized after asking. Maybe redirect this explanation to spinon?
Mike Atlas
I believe it notifies them as well as you.
Steven Sudit
You know I really didn't think about it in the context of needing the data in the collection so I just went with the problem at hand.
spinon
@spinon: That accounts for it. One of my pet peeves about SO is that it lends itself to a shallow reading of the question, and sometimes actively punishes responses that do not agree with the hidden premises behind the question.
Steven Sudit
@Steven you're right. Often I tend to take the shallow reading as well because it could be all for not if I try to speculate and put more into my answer. I feel, if they want a better answer they need to provide a better question. But you're right about being punished. That happened the other day for a question that had no detail. Later they edited and added the detail and then my answer looked completely off from what they wanted. Someone hit me up about why I was so off. I didn't understand what he was talking about until I saw there was an edit and it was almost an entirely new question.
spinon
@spinon: Please don't take it personally, as I'm often as guilty of this as you were. It's a systemic problem. What usually hits me is when they ask how to do something when in fact they really need to do something else in order to achieve the same goal. I tend to get downvoted for correct answers then.
Steven Sudit
@Steven yeah I don't. I figure I'm not getting paid for anything to contribute here. So I'll just offer some help. If you want it take it. If you don't, then don't.
spinon
+1  A: 

Are you using Variable1 later in your code, or just to find the row count.

If the latter, it's just:

int RowTotal = DB.Table.Count();

If for the full block:

int RowTotal = (Something == 0) ? DB.Table.Count() : 0;
James Curran