Initialize var to null
Asked Answered
R

7

8

I have seen how to initialize var to null. This does not help in my situation. I have

string nuller = null;
var firstModel = nuller;
if(contextSelectResult.Count() > 0)
    firstModel = contextSelectResult.First();

I get error

Cannot implicitly convert type 'SomeNamespace.Model.tableName' to 'string'.

I am trying to avoid try/catching InvalidOperation for First() when no first exists as its expensive. So, how can I get past the scope issue here?

Reider answered 24/5, 2011 at 14:22 Comment(4)
Try this: firstModel = contextSelectResult.First().ToString()Setting
If you skip the count and just use FirstOrDefault() you should get a null anyway.Kentiggerma
Do you want firstModel to be a string or a more complex data object? As you have defined it, it is a string (and the error message is telling you that)Magpie
@Mark Peters - Right. That's just to further clarify my point that the link I posted does not help me. Second, I am using linq so I don't want to use anything other than var. So, since I don't know my return type (or want to know) until compile time I want an answer that acknowledges these facts. As the answers by Jon and Bala do.Reider
O
6

Simply use FirstOrDefault() instead. The whole point of FirstOrDefault is to return the first element of the sequence if it exists, or the default value of the element type (i.e. null for all reference types) otherwise.

Note that in other cases where you wish to check for the existence of any elements, using Any() can sometimes be more efficient than Count() > 0 - it depends on the exact context, but IMO it's a simpler way of expressing what you're looking for anyway.

Ore answered 24/5, 2011 at 14:24 Comment(0)
T
26

You can try this:

var firstModel=(dynamic) null; 
Twoply answered 4/4, 2012 at 8:56 Comment(2)
I don't see any benefits of using dynamic here. You'll even have an overhead because of the dynamic behavior.Kimmy
Or dynamic firstModel = null;Grados
F
7

You can use FirstOrDefault() instead.

firstModel = contextSelectResult.FirstOrDefault();

if(firstModel != null)
{
   ...
}
Fanechka answered 24/5, 2011 at 14:23 Comment(0)
O
6

Simply use FirstOrDefault() instead. The whole point of FirstOrDefault is to return the first element of the sequence if it exists, or the default value of the element type (i.e. null for all reference types) otherwise.

Note that in other cases where you wish to check for the existence of any elements, using Any() can sometimes be more efficient than Count() > 0 - it depends on the exact context, but IMO it's a simpler way of expressing what you're looking for anyway.

Ore answered 24/5, 2011 at 14:24 Comment(0)
G
2

Try FirstOrDefault instead. It returns nullby default if there is no item.

Gallows answered 24/5, 2011 at 14:24 Comment(0)
B
2

Please Try this option:

var var_name = (dynamic)null; 

or

var var_name = (Type*)null;

Type* : eg --> string, var, int

Blastomere answered 15/12, 2015 at 10:40 Comment(0)
K
1

If there is no First it'll be a null for reference types:

var firstModel = contextSelectResult.FirstOrDefault();
Kentiggerma answered 24/5, 2011 at 14:26 Comment(0)
D
0

You can use the generic for this case also

public static dynamic GetTheListOfDevicesDependOnDB(int projectID)
{
    List<Devices_Settings> ListDevices_Settings = new List<Devices_Settings>();
    var db = new First_DataContext();
    var devices = (dynamic) null;
    switch (projectID)
    {
        case (int)enmProjectType.First:
            db = new First_DataContext();
            devices = db.Device_Fisrt.ToList();   
            break;
        case (int)enmProjectType.Second:
             var db1 = new Second_DataContext();
             devices = db1.Device_Second.ToList();
            break;

        default:
            break;
    }

    foreach (var item in devices)
    {
       //TODO
    }

    return ListDevices_Settings;

}

Dilworth answered 25/6, 2013 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.