score:4
As hinted at in Jeremy's answer, If you've a reference to System.Data.DataSetExtensions.dll, you'll get some handy extension methods for working with DataSets and DataTables using LINQ. Specifically, you can use the Field<int?>
() method to convert an integer column that might contain DBNull into a column of nullable ints...
albumIds = dt.AsEnumerable().Select(row => row.Field<int?>("F1"))
.Where(val => val.HasValue)
.Select(val => val.Value)
.Distinct()
.ToArray();
score:0
You just need to filter the null values out first. Then your LINQ expression should work great.
score:0
You need to check the fields for DBNull before you attempt the conversion using the Where method is probably easiest.
dt.AsEnumerable().Where(p => p.Field("F1") != DBNull.Value).Select(p => (int)p.Field("F1")).Distinct().ToArray();
score:-1
I'll answer my own question.
Instead of using Linq I do a foreach loop and delete rows if value is null. and then do Linq distinct
foreach(DataRow row in dt.Rows)
{
if (String.IsNullOrEmpty(row["F1"].ToString()))
row.Delete();
}
dt.AcceptChanges();
albumIds = dt.AsEnumerable().Select(p => (int)p.Field<double>("F1")).Distinct().ToArray();
score:0
Can you try this:
dt.AsEnumerable().Where(p => p.IsNull("F1") == false)
.Select(p => p.Field<int>("F1")).Distinct().ToArray();
I can't really check this as I don't have this DB setup
Source: stackoverflow.com
Related Articles
- Filer DataTable to exclude DBNull.Value
- Partition By Logic in Code to calculate value of a DataTable Column
- Value cannot be null. Parameter name: source
- How to resolve Value cannot be null. Parameter name: source in linq?
- Linq : select value in a datatable column
- How to Convert the value in DataTable into a string array in c#
- Linq query to exclude from a List when a property value of List of different type are equal?
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- SQL Trigger is trying to insert a null value but my C# code is passing int 300?
- The given value of type String from the data source cannot be converted to type int of the specified target column
- How to check DBNull value in Linq query results
- skip entire row if particular cell value contains zero in datatable C#
- C# - Remove rows with the same column value from a DataTable
- Linq to DataTable - Cannot cast DBNull
- Exclude one value in LINQ OrderBY
- How to Create a Run-Time Computed (NotMapped) Value in Entity Framework Code First
- creating Linq to sqlite dbml from DbLinq source code
- C# - Code supposed to be unreachable when calculate value in LINQ sum method
- linq datatable find range of value occuring
- Linq way to find value in DataTable
- LINQ to remove duplicate rows from a datatable based on the value of a specific row
- .Net - Find max value in DataTable
- Querying for a specific value at the intersection of a Row and a Column in a DataTable
- Remove datatable row if any column value is empty c#
- DataTable Column value grouping and sorting
- Sort the value with DBNull using Queryable.OrderBy it throws Exception
- Can't add a new record with an integer value into database by using linq from code C#
- How to prevent error conversion from DBNull value to Decimal in Linq query results
- Get Max from column datatable or Default Value using Linq
- Linq query syntax
- Linq SubSelect with Expression Trees
- Translate from lambda (method) syntax to query syntax (LINQ)
- Full outer join Linq
- How to check for the duplicates in an ObservableCollection?
- Linq Take on Include?
- How do you use (or can you use) the Linq Intersect method on a child list member of an IEnumerable?
- Object reference not set to an instance of an object. yet foreach loop is working
- Dynamic linq expression for filtering with enum collection
- Can we write Update statement in linq?
- ListView containing CheckBoxList - Selected items not showing as checked
- How to query a lookup table using a list?
- Linq dynamic select column and table
- EF DatabaseIntializer looping through list methods with overloads,
- Alternative to Contains method in Generics
- Why does calling IEnumerable<string>.Count() create an additional assembly dependency?
- LINQ Database
- C#--Counting the number of occurrences of a single digit(int) in a string--not working with int to char converted Variable
- Parse csv or excel file in Silverlight
- How to reuse a data into an anonymous type decalaration?