public
DataTable SortVesselDashBoard(
bool
Completed,
bool
WithFindings,
string
orderType)
bool
oCompleted =
true
;
bool
oWithFindings =
true
;
oCompleted = Completed;
oWithFindings = WithFindings;
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
;
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.
See if this works. Create a function that can take in your object and return a property back:
Func<SafetyData, object> orderByValue = sd =>
typeof
(SafetyData).GetProperty(
"
Year"
).GetValue(sd);
Now order by that function:
.OrderBy(safetyData => orderByValue(safetyData))
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
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();