**1、返回实体**
~~~
UserTab user = new UserTab() { UserID = 1 };
OQL q = OQL.From(user)
.Select()
.Where(user.UserID)
.END;
UserTab dbUser = EntityQuery<UserTab>.QueryObject(q);
~~~
根据主键得到实体
~~~
/// <summary>
/// 得到一个实体对象
/// </summary>
/// <param name="id">单主键</param>
/// <returns>entity or null</returns>
public static EntityA GetEntity(T id)
{
EntityA entity = new EntityA();
entity.ID= id;
return EntityQuery<EntityA>.Fill(entity);
}
~~~
根据主键得到泛型实体
~~~
/// <summary>
/// 得到一个实体对象
/// </summary>
/// <param name="primaryKeys">单主键名</param>
/// <param name="id">单主键</param>
/// <returns>entity or null</returns>
public static T GetEntity<U>(string primaryKey, U id)
{
T entity = new T();
entity[primaryKey] = id;
var result = EntityQuery<T>.Fill(entity);
}
~~~
**2、返回实体集合**
~~~
UserTab user = new UserTab();
OQL q = OQL.From(user)
.Select() //返回所有列
.Where(cmp => cmp.Comparer(user.UserName, OQLCompare.CompareType.Like, "%" + "x" + "%")
& cmp.Comparer(user.UserSex, OQLCompare.CompareType.Equal, "男"))
.OrderBy(user.UserID, "asc")
.END;
List<UserTab> users = EntityQuery<UserTab>.QueryList(q);
~~~
这里用到了排序 OrderBy
Select() 代表取所有列,如果只要返回某些字段信息则写在Select里 :
~~~
Select(user.UserID ,user.UserName)
~~~
如果要返回所有用户表集合,一句代码即可:
~~~
var userList=OQL.From<UserTab>().Select().End.ToList<UserTab>();
~~~