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:-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
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: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 Query
- 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
More Query from same tag
- Case sensitive in linq Query
- How to filter a Linq Query from XML to IEnumerable of Objects
- Linq query for filter object graph
- LINQ and DataTable query
- LINQ Group By if an object has x number of properties, then select on each group
- How to access a particular data in LINQ query result?
- Group by multiple fields C# with object List
- Extension method for Sorting a generic iCollection(Of T)
- LINQ to XML - parent's attribute in where clause
- Determine table during runtime with Linq
- linq to xml - get rid of blank xmlns
- Group items and select specific item from each group with LINQ
- Linq Errors on ToList(), Any(), Count(), Sum()
- Sorting a list in .Net by one field, then another
- Turn 2 linq expressions into
- How to retrieve multiple rows from a stored procedure with Linq to SQL?
- Operator '=' is not defined for types 'Integer' and 'IQueryable(Of Integer)'
- Left join multiple tables using lambda expression
- Simplify Entity Framework Core query
- C# is there a better way of adding an element from a list in another class?
- add all values to a list of list from a list
- Collections manipulation, need help optimizing this code from a report generator
- C# Linq Convert Object Property to Array
- Count word occurrences in a text field with LINQ
- LINQ query with Distinct and Union
- "OR " condition in where clause of linq query is returning null
- LINQ not equal doesn't work
- Specified cast not valid calling stored procedure
- Displaying Data in GridView on click event, Search Page
- Linq to NHibernate ordering is not working as expected