添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
玩足球的铅笔  ·  图像处理 ...·  7 月前    · 
酒量大的烈酒  ·  Python ...·  8 月前    · 
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))
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();
  • 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.
  •