Easty
Easty

Reputation: 387

linq to entity query getting error

I am trying to amend a c# project. I am a vb.vet programmer so having a few issues as I am new to linq. I am trying to run a Linq to Entity query. I want to select the MapEast where town = town. I keep get an error The specified cast from a materialized System.Decimal' type to the 'System.Int32' type is not valid.. I would like to put a max(1) in here too so it returns only the highest number.

var topEast = 0;
try
{
    topEast = this._uow.Addresses
            .Where(a => 
                a.Town.Trim().ToUpper() == town.Trim().ToUpper())
            .Select(m => m.MapEast).FirstOrDefault ();
    return -1;
}
catch
{
    return -1;
}

Thanks

Upvotes: 0

Views: 487

Answers (1)

Habib
Habib

Reputation: 223187

var is used for implicitly typed local variable. When you defined var topEast = 0;, topEast was implicitly assigned type int, and not decimal as per your query. You can fix it by explicitly defining topEast as decimal.

decimal topEast = 0;

I would like to put a max(1) in here too so it returns only the highest number.

Not really sure what you are trying to return, because you are returning -1 from try as well as catch block. If you are trying to return the Max value of MapEast field then you will need Enumerable.Max, otherwise FirstOrDefault would return the first item or null based on criteria.

Upvotes: 3

Related Questions