原文:DevExpress ASP.NET 使用经验谈(4)-CriteriaOperator的使用
上一节中,我们已经介绍了,使用CriteriaOperator表达式,获取对象数据。
CriteriaOperator criteria = CriteriaOperator.Parse("[UserID]=‘" + obj.UserID + "‘"); Users objnew = session.FindObject<Users>(criteria);
如果查询结果是多条数,我们可以使用XPCollection来接收:
XPCollection<Users> coll = new XPCollection<Users>(session); CriteriaOperator criteria = CriteriaOperator.Parse(""); SortProperty sortProperty = new SortProperty("FirstName", SortingDirection.Ascending); SortingCollection s = new SortingCollection(sortProperty); coll.Session = session; coll.Criteria = criteria; coll.Sorting = s;
我们修改一下上节的例子,修改后的代码如下:
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using DevExpress.Data.Filtering; using DevExpress.Xpo; using DevExpress.Xpo.DB; using XPOModel.DemoDB; namespace DevConsole { class Program { static void Main(string[] args) { string provider = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;//获取数据库连接 IDataLayer datalayer = new SimpleDataLayer(XpoDefault.GetConnectionProvider(provider, AutoCreateOption.DatabaseAndSchema));//建立数据层XPO独有的 DevExpress.Xpo.Session session = new DevExpress.Xpo.Session(datalayer); //将数据层和会话绑定 for (int i = 0; i < 10; i++) { Users obj = new Users(session); obj.FirstName = "Dave_" + i.ToString(); obj.LastName = "Annable"; obj.EmailID = "[email protected]"; obj.Save(); } XPCollection<Users> coll = new XPCollection<Users>(session); CriteriaOperator criteria = CriteriaOperator.Parse(""); SortProperty sortProperty = new SortProperty("FirstName", SortingDirection.Ascending); SortingCollection s = new SortingCollection(sortProperty); coll.Session = session; coll.Criteria = criteria; coll.Sorting = s; for (int j = 0; j < coll.Count; j++) { Users obj = coll[j]; Console.WriteLine(string.Format("ID:{0},FirstName:{1},LastName:{2}", obj.UserID, obj.FirstName, obj.LastName)); } Console.ReadLine(); } } }
运行程序后,控制台显示如下:
图一 执行查询结果
CriteriaOperator还可以应用于XpoDataSource(后续将提到如何使用)。
前台添加XpoDataSource控件如下:
<dx:XpoDataSource ID="XpoDataSource1" runat="server" ServerMode="True" TypeName="XPOModel.DemoDB.Users"></dx:XpoDataSource>
后台代码指标查询过滤条件如下:
XpoDataSource1.Session = session; XpoDataSource1.Criteria = "UserID=‘122008‘";
展现效果如下:
图二 XpoDataSource执行过滤查询后结果
下一节,我们将通过一个完整示例,介绍如何使用DevExpress控件,通过非常少的代码实现CRUD操作...
时间: 2024-10-07 18:29:00