Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i created a simple linq that can select data with sorting data from ms sql to c#

public DataTable SortVesselDashBoard(bool Completed, bool WithFindings, string orderType)
       {
           bool oCompleted = true;
           bool oWithFindings = true;
           oCompleted = Completed;
           oWithFindings = WithFindings;
           try
           {
               using (vesselSafetyDBDataContext oConnection = new vesselSafetyDBDataContext())
               {
                   var oData = (from safetyData
                               in oConnection.tbl_SafetyDatas
                                where Completed == oCompleted
                                && safetyData.WithFindings == oWithFindings
                                select safetyData).OrderByDescending(x => x.Year);


                   return LINQResultToDataTable(oData);
               }
           }
           catch (Exception ex)
           {
               throw;
           }
       }




As you see I select all the data from
tbl_SafetyDatas
and order it by Year
what i want is the below code where i can declare it dynamical, i want to avoid
creating a multiple methods.

var oData = (from safetyData
                                in oConnection.tbl_SafetyDatas
                                 where Completed == oCompleted
                                 && safetyData.WithFindings == oWithFindings
                                 select safetyData).OrderBy("Year");


What I have tried:

i tried using
System.Linq.Dynamic.Core;
but i don't know how to use it properly or does it work with linq to sql? I'm new to Linq.
Posted
Updated 11-Jun-21 4:04am

1 solution

See if this works. Create a function that can take in your object and return a property back:

C#
Func<SafetyData, object> orderByValue = sd => typeof(SafetyData).GetProperty("Year").GetValue(sd);


Now order by that function:

C#
.OrderBy(safetyData => orderByValue(safetyData))
 
Share this answer
 
Comments
Richard Deeming 11-Jun-21 10:43am    
That will only work if you load all of the data into memory first. When Linq2Sql tries to convert that to a SQL query, it will fail.
gardnerp 11-Jun-21 11:06am    
Yes. Pull data, then sort. If for some reason you need the database to handle it, you can use a switch statement. That way the full query can be setup in advance and not fully repeated.

var linq2SqlQuery = (from obj in table where obj.Whatever = "Some filter" select obj);

switch (propertyToSort) {
case "Year":
linq2SqlQuery = linq2SqlQuery.OrderBy(o => o.Year);
break;
case "OtherProp":
linq2SqlQuery = linq2SqlQuery.OrderBy(o => o.OtherProp);
break;
}

var results = linq2SqlQuery.ToList();

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