C# 如何在LinQ中按降序显示orderby
我想通过web服务在C# 如何在LinQ中按降序显示orderby,c#,linq,C#,Linq,我想通过web服务在LINQ中降序显示订单,我有一个LINQ查询,我想按最近创建的时间排序 这里是我的web服务(GetContent.asmx.cs): [WebMethod] 公共字符串[]GetContentText(字符串NamaPage、字符串TanggalAkses、字符串键) { string[]ret=新字符串[4]{null,null,null,null}; if(clsEncrypt.DecodeFrom64(键)=TanggalAkses) { FrontEndConten
LINQ
中降序显示订单,我有一个LINQ
查询,我想按最近创建的时间排序
这里是我的web服务(GetContent.asmx.cs):
[WebMethod]
公共字符串[]GetContentText(字符串NamaPage、字符串TanggalAkses、字符串键)
{
string[]ret=新字符串[4]{null,null,null,null};
if(clsEncrypt.DecodeFrom64(键)=TanggalAkses)
{
FrontEndContent dt=db.FrontEndContent.FirstOrDefault(m=>m.NamaPage.ToLower()==NamaPage.ToLower().OrderByDescending(m=>m.CreatedTime.ToList());
//List dt=db.FrontEndContent.OrderByDescending(m=>new{m.CreatedTime}).ToList();
如果(dt!=null)
{
ret[0]=dt.Id.ToString();
ret[1]=dt.HeaderText;
if(String.IsNullOrEmpty(ret[1]))
{
ret[1]=“”;
}
ret[2]=dt.分标题文本;
if(String.IsNullOrEmpty(ret[1]))
{
ret[2]=“”;
}
ret[3]=dt.ContentText;
if(String.IsNullOrEmpty(ret[1]))
{
ret[3]=“”;
}
}
}
返回ret;
}
我的模型是:
using System;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace penerimaan.Models
{
public class FrontEndContent
{
private ModelEntities db = new ModelEntities();
[Key]
[DisplayName("ID")]
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required(ErrorMessage = "Nama Page is Required")]
[DisplayName("Nama Page")]
[StringLength(110)]
public string NamaPage { get; set; }
[DisplayName("Header Text")]
[StringLength(110)]
public string HeaderText { get; set; }
[DisplayName("Sub Header Text")]
[StringLength(110)]
public string SubHeaderText { get; set; }
[DisplayName("Content")]
[StringLength(10000)]
[AllowHtml]
public string ContentText { get; set; }
[DisplayName("Created Time")]
public System.DateTime? CreatedTime { get; set; }
[DisplayName("Updated Time")]
public System.DateTime? UpdatedTime { get; set; }
[DisplayName("Created By")]
public int? CreatedBy_Id { get; set; }
[DisplayName("Updated By")]
public int? UpdatedBy_Id { get; set; }
public IEnumerable<FrontEndContent> SelectData()
{
List<FrontEndContent> lst = new List<FrontEndContent>();
lst = db.FrontEndContent.OrderByDescending(m => new { m.NamaPage }).ToList();
lst = db.FrontEndContent.OrderByDescending(m => new { m.CreatedTime }).ToList();
return lst;
}
}
}
使用系统;
使用System.Linq;
使用系统组件模型;
使用System.ComponentModel.DataAnnotations;
使用System.Web;
使用System.Web.Mvc;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations.Schema;
名称空间penerimaan.Models
{
公共类前端内容
{
私有模型实体db=新模型实体();
[关键]
[显示名称(“ID”)]
[脚手架立柱(假)]
公共int Id{get;set;}
[必需(ErrorMessage=“需要Nama页面”)]
[显示名称(“Nama页面”)]
[长度(110)]
公共字符串NamaPage{get;set;}
[显示名称(“标题文本”)]
[长度(110)]
公共字符串头文本{get;set;}
[显示名称(“子标题文本”)]
[长度(110)]
公共字符串子标题文本{get;set;}
[显示名称(“内容”)]
[长度(10000)]
[allowtml]
公共字符串ContentText{get;set;}
[显示名称(“创建时间”)]
public System.DateTime?CreatedTime{get;set;}
[显示名称(“更新时间”)]
public System.DateTime?updatetime{get;set;}
[显示名称(“创建人”)]
public int?CreatedBy_Id{get;set;}
[显示名称(“更新人”)]
public int?由_Id{get;set;}更新
公共IEnumerable SelectData()
{
List lst=新列表();
lst=db.FrontEndContent.OrderByDescending(m=>new{m.NamaPage}).ToList();
lst=db.FrontEndContent.OrderByDescending(m=>new{m.CreatedTime}).ToList();
返回lst;
}
}
}
但这会产生一个错误:
“char”不包含“CreatedTime”的定义,并且找不到接受“char”类型的第一个参数的扩展方法“CreatedTime”(是否缺少using指令或程序集引用?)
改用这个:
FrontEndContent dt = db.FrontEndContent.Where(m => m.NamaPage.ToLower() == NamaPage.ToLower()).OrderByDescending(m => m.CreatedTime).FirstOfDefault();
改用这个:
FrontEndContent dt = db.FrontEndContent.Where(m => m.NamaPage.ToLower() == NamaPage.ToLower()).OrderByDescending(m => m.CreatedTime).FirstOfDefault();
为什么在订购时要使用新关键字?您不需要这样做。 这应该很好用
List<FrontEndContent> lst = new List<FrontEndContent>();
lst = db.FrontEndContent.OrderByDescending(m => m.NamaPage).ToList();
lst = db.FrontEndContent.OrderByDescending(m => m.CreatedTime).ToList();
return lst;
List lst=new List();
lst=db.FrontEndContent.OrderByDescending(m=>m.NamaPage.ToList();
lst=db.FrontEndContent.OrderByDescending(m=>m.CreatedTime.ToList();
返回lst;
订购时为什么要使用新关键字?您不需要这样做。
这应该很好用
List<FrontEndContent> lst = new List<FrontEndContent>();
lst = db.FrontEndContent.OrderByDescending(m => m.NamaPage).ToList();
lst = db.FrontEndContent.OrderByDescending(m => m.CreatedTime).ToList();
return lst;
List lst=new List();
lst=db.FrontEndContent.OrderByDescending(m=>m.NamaPage.ToList();
lst=db.FrontEndContent.OrderByDescending(m=>m.CreatedTime.ToList();
返回lst;
您得到的错误可以通过在谓词中不使用new
关键字来修复
List<FrontEndContent> lst = new List<FrontEndContent>();
lst = db.FrontEndContent.OrderByDescending(m => m.NamaPage).ToList();
lst = db.FrontEndContent.OrderByDescending(m => m.CreatedTime).ToList();
return lst;
在WebService
上,您错过了一个parentesis,而且ToList
方法应该在排序操作之后
FrontEndContent dt = db.FrontEndContent.Where(m => m.NamaPage.ToLower() == NamaPage.ToLower()).OrderByDescending(m => m.CreatedTime).ToList();
您得到的错误可以通过在谓词中不使用
new
关键字来修复
List<FrontEndContent> lst = new List<FrontEndContent>();
lst = db.FrontEndContent.OrderByDescending(m => m.NamaPage).ToList();
lst = db.FrontEndContent.OrderByDescending(m => m.CreatedTime).ToList();
return lst;
在WebService
上,您错过了一个parentesis,而且ToList
方法应该在排序操作之后
FrontEndContent dt = db.FrontEndContent.Where(m => m.NamaPage.ToLower() == NamaPage.ToLower()).OrderByDescending(m => m.CreatedTime).ToList();
不为字符串排序。为集合排序。使用
FrontEndContent dt=db.FrontEndContent.Where(m=>m.NamaPage.ToLower()==NamaPage.ToLower()).OrderByDescending(m=>m.CreatedTime.ToList()).FirstOfDefault();
不为字符串排序。为集合排序。使用FrontEndContent dt=db.FrontEndContent.Where(m=>m.NamaPage.ToLower()==NamaPage.ToLower()).OrderByDescending(m=>m.CreatedTime.ToList()).FirstOfDefault();
我认为这段代码不会编译,因为您在CreatedTime属性上调用了ToList()方法。也许您必须在闭括号后调用它。所以在调用FirstOrDefault()时使用ToList()是多余的在那之后。我认为这段代码不会编译,因为您在CreatedTime属性上调用了ToList()方法。也许您需要在闭括号后调用它。因此,在调用FirstOrDefault()之后使用ToList()是多余的。