I am using C#, Linq to Entities and .Net 4
I am using the following code to extract a list of data. The data may be a number but for some records it may be null or some other form of data. Is it possible with Linq to extract just the numbers in this filed or do I need to use additional steps?
var s = (from t in context.Cards where t.CardData != null select t.CardData).ToList();
The above will return a list of strings but I’d like a list of int. The CardData field may be null, a number or some text. In the DB it is a nullable nvarchar field.
The simplest way to do this will be to use
TryParseon each element of your returned list. You’ve already eliminated the null elements so you know you have something:This has the added benefit that you’ll understand what it does 18 months from now.