C# 使用SQL查询中的条件查询连接从数据库检索数据
我有一个类,用来携带过滤数据。我想根据筛选选项从数据库检索数据。 我的FilteringTo类:C# 使用SQL查询中的条件查询连接从数据库检索数据,c#,nhibernate,C#,Nhibernate,我有一个类,用来携带过滤数据。我想根据筛选选项从数据库检索数据。 我的FilteringTo类: public class FilteringDto { public int id { get; set; } public string search_text { get; set; } } 我想从CafeTableGroup表中检索数据。我的查询是这样的: using (ISession session = SessionFactory.
public class FilteringDto
{
public int id { get; set; }
public string search_text { get; set; }
}
我想从CafeTableGroup
表中检索数据。我的查询是这样的:
using (ISession session = SessionFactory.OpenSession)
{
using (ITransaction transaction = session.BeginTransaction())
{
groups = session.CreateCriteria<CafeTableGroup>().List<CafeTableGroup>();
if (string.IsNullOrEmpty(filters.search_text))
{
groups = groups.Where(a => a.field_1.Like(filters.search_text)).ToList();
}
if (filters.id != 0)
{
groups = groups.Where(a => a.field_2== filters.id).ToList();
}
transaction.Commit();
}
}
使用(ISession session=SessionFactory.OpenSession)
{
使用(ITransaction transaction=session.BeginTransaction())
{
groups=session.CreateCriteria().List();
if(string.IsNullOrEmpty(filters.search_text))
{
groups=groups.Where(a=>a.field_1.Like(filters.search_text)).ToList();
}
如果(filters.id!=0)
{
groups=groups.Where(a=>a.field_2==filters.id).ToList();
}
Commit();
}
}
但我这里有个问题。为了获得过滤数据,它首先检索表中的所有数据,然后根据条件对其进行过滤。是否有任何方法可以使用单个查询来检索过滤后的数据而不是所有数据?提前感谢。代码中的问题是
.List()代码>这会导致过早实现实例。只需延迟调用列表
我没有用你的确切例子。此外,我的代码使用IQueryOver
而不是CreateCriteria
。您可以通过以下代码实现这一点:
public IList<Table1Entity> GetList(FilterParams filterParams = null, PageParams pageParams = null)
{
IList<Table1Entity> instance = null;
Conjunction conjTable1 = Restrictions.Conjunction();
Conjunction conjTable2 = Restrictions.Conjunction();
if(filterParams == null)
filterParams = new FilterParams();
if(!string.IsNullOrEmpty(filterParams.Date))
conjTable1.Add(Restrictions.Eq(Projections.Property<Table1Entity>(x => x.Date), filterParams.Date));
if(!string.IsNullOrEmpty(filterParams.FromTime))
conjTable1.Add(Restrictions.Eq(Projections.Property<Table1Entity>(x => x.FromTime), filterParams.FromTime));
if(!string.IsNullOrEmpty(filterParams.ToTime))
conjTable1.Add(Restrictions.Eq(Projections.Property<Table1Entity>(x => x.ToTime), filterParams.ToTime));
if(!string.IsNullOrEmpty(filterParams.Id))
conjTable1.Add(Restrictions.Eq(Projections.Property<Table1Entity>(x => x.Id), Guid.Parse(filterParams.Id)));
if(!string.IsNullOrEmpty(filterParams.Pid))
conjTable2.Add(Restrictions.Eq(Projections.Property<Table2Entity>(x => x.Pid), Guid.Parse(filterParams.Pid)));
IQueryOver<Table1Entity> query = NHSession.QueryOver<Table1Entity>()
.Where(conjTable1)
.JoinQueryOver(x => x.Table2)
.And(conjTable2);
if(pageParams != null)
query = query.Skip(pageParams.SkipRecords).Take(pageParams.TakeRecords);
instance = query.List();
return instance;
}
public IList GetList(FilterParams FilterParams=null,PageParams PageParams=null)
{
IList实例=null;
连接表1=限制。连接();
连接表2=限制。连接();
if(filterParams==null)
filterParams=新的filterParams();
如果(!string.IsNullOrEmpty(filterParams.Date))
表1.Add(Restrictions.Eq(projects.Property(x=>x.Date),filterParams.Date));
如果(!string.IsNullOrEmpty(filterParams.FromTime))
连接表1.Add(Restrictions.Eq(projects.Property(x=>x.FromTime),filterParams.FromTime));
如果(!string.IsNullOrEmpty(filterParams.ToTime))
contactable1.Add(Restrictions.Eq(projects.Property(x=>x.ToTime),filterParams.ToTime));
如果(!string.IsNullOrEmpty(filterParams.Id))
contactable1.Add(Restrictions.Eq(Projections.Property(x=>x.Id)、Guid.Parse(filterParams.Id));
如果(!string.IsNullOrEmpty(filterParams.Pid))
Add(Restrictions.Eq(Projections.Property(x=>x.Pid)、Guid.Parse(filterParams.Pid));
IQueryOver query=NHSession.QueryOver()
.其中(表1)
.JoinQueryOver(x=>x.Table2)
.及(表2);
如果(pageParams!=null)
query=query.Skip(pageParams.SkipRecords).Take(pageParams.TakeRecords);
instance=query.List();
返回实例;
}
这还演示了如何实现连接和分页。先生,我什么都不懂。NHibernate是否仅在调用.ToList()后检索数据?是的,您调用List
太早了。推迟通话。仔细阅读我的示例代码。当我添加完所有过滤器后,它在末尾调用List
。