Click here to Skip to main content
15,944,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a linq query that will fit priority sorting. However, not all tables have the same columns.

  Public Function Custom_sort(dt As DataTable) As DataView

      Custom_sort = dt.AsEnumerable().
        OrderBy(Function(row) String.IsNullOrWhiteSpace(row.Field(Of String)("Employee_SSN"))).

if dt.columns.exists("Group_number") then
ThenBy(Function(row) String.IsNullOrWhiteSpace(row.Field(Of String)("Group_number"))).
end if


return custom_sort

end function

What I have tried:

I have tried the above-written statement and I have tried an encapsulated if() statement, but neither worked.
Updated 27-Nov-22 23:37pm
[no name] 27-Nov-22 12:04pm    
You want a "generic entity class" for different tables. LINQ deals with entities. How you fill those entities and from which tables, is up to you. It would (probably) only work in a query capacity unless you want to dig a realy deep hole. Think again why you think you need a "generic entity" (e.g. false economy).

1 solution

You need to build up the query in a local variable:
Public Function Custom_sort(ByVal dt As DataTable) As DataView
    Dim rows As OrderedEnumerableRowCollection = dt.AsEnumerable().
        OrderBy(Function(row) String.IsNullOrWhiteSpace(row.Field(Of String)("Employee_SSN")))
    If dt.Columns.Contains("Group_number") Then
        rows = rows.ThenBy(Function(row) String.IsNullOrWhiteSpace(row.Field(Of String)("Group_number")))
    End If
    Return rows.AsDataView()
End Function
Share this answer
Member 11856456 29-Nov-22 7:05am    
Thank you for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900