跨多个实体查询
我有一个奇怪的要求,我不知道如何解决。
假设以下类定义:
public class Client
{
public Guid Id { get; set; }
public String Person { get; set; }
public IList<Client> SecondaryClients { get; set; }
public Client PrimaryClient { get; set; }
}
public class Person
{
public Guid Id { get; set; }
public String Name { get; set; }
}
当用户按名称在系统中搜索客户端时,需要搜索所有主客户端以及一跳之外的辅助客户端。 (即,如果设置了 PrimaryClient,那么我们需要检查 PrimaryClient.Person.Name 属性,但是我们不必担心 PrimaryClient.PrimaryClient。)
使用 DetachedCriteria,我有以下结果:
var clientQuery = DetachedCriteria.For<Client>();
clientQuery.Add(Restrictions.Disjunction()
.Add(Restrictions.Like("Person.Surname", lastName, MatchMode.Start))
.Add(Restrictions.Like("PrimaryClient.Person.Surname", lastName, MatchMode.Start))
.Add(Restrictions.Like("SecondaryClients.Person.Surname", lastName, MatchMode.Start)));
var session = OpenSession();
session.BeginTransaction();
var clients = clientQuery.GetExecutableCriteria(session).Future<Client>();
session.Transaction.Commit();
session.Close();
现在显然,这还很遥远。经过一番挖掘,我发现我需要设置别名。第一个很容易找到 Person.Surname:
var clientQuery = DetachedCriteria.For<Client>();
clientQuery = clientQuery.CreateAlias("Person", "p");
clientQuery.Add(Restrictions.Disjunction()
.Add(Restrictions.Like("p.Surname", lastName, MatchMode.Start))
.Add(Restrictions.Like("PrimaryClient.Person.Surname", lastName, MatchMode.Start))
.Add(Restrictions.Like("SecondaryClients.Person.Surname", lastName, MatchMode.Start)));
var session = OpenSession();
session.BeginTransaction();
var clients = clientQuery.GetExecutableCriteria(session).Future<Client>();
session.Transaction.Commit();
session.Close();
但是,对于我的一生,我不确定我能为 PrimaryClient.Person 的别名做什么。我在这里走错路了吗?任何帮助将不胜感激。
注意:我忘了最初提及。 secondaryClients 和 PrimaryClient 可能为空。
I have a strange requirement I'm not sure how to solve.
Assuming the following class definition:
public class Client
{
public Guid Id { get; set; }
public String Person { get; set; }
public IList<Client> SecondaryClients { get; set; }
public Client PrimaryClient { get; set; }
}
public class Person
{
public Guid Id { get; set; }
public String Name { get; set; }
}
When a user searches the system for a client by name, it needs to search all primary clients as well as secondary clients that are one hop away. (ie if the PrimaryClient is set then we need to check the PrimaryClient.Person.Name property, however we don't have to worry about the PrimaryClient.PrimaryClient.)
Using DetachedCriteria, I have the following:
var clientQuery = DetachedCriteria.For<Client>();
clientQuery.Add(Restrictions.Disjunction()
.Add(Restrictions.Like("Person.Surname", lastName, MatchMode.Start))
.Add(Restrictions.Like("PrimaryClient.Person.Surname", lastName, MatchMode.Start))
.Add(Restrictions.Like("SecondaryClients.Person.Surname", lastName, MatchMode.Start)));
var session = OpenSession();
session.BeginTransaction();
var clients = clientQuery.GetExecutableCriteria(session).Future<Client>();
session.Transaction.Commit();
session.Close();
Now obviously, this is way off. Doing some digging around I discovered that I needed to setup Aliases. The first was easy for finding the Person.Surname:
var clientQuery = DetachedCriteria.For<Client>();
clientQuery = clientQuery.CreateAlias("Person", "p");
clientQuery.Add(Restrictions.Disjunction()
.Add(Restrictions.Like("p.Surname", lastName, MatchMode.Start))
.Add(Restrictions.Like("PrimaryClient.Person.Surname", lastName, MatchMode.Start))
.Add(Restrictions.Like("SecondaryClients.Person.Surname", lastName, MatchMode.Start)));
var session = OpenSession();
session.BeginTransaction();
var clients = clientQuery.GetExecutableCriteria(session).Future<Client>();
session.Transaction.Commit();
session.Close();
However, for the life of me I'm not sure what I can do for the alias of PrimaryClient.Person. Am I going down the wrong path here? Any help would be appreciated.
NOTE: I forgot to mention originally. It is possible that SecondaryClients and PrimaryClient are null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于那些记分的人来说,我能够弄清楚如何做到这一点。我不确定是否有更有效的方法,但是以下是我使用 DetachedCriteria 设置查询的方法。
For those keeping score, I was able to figure out how to do this. I'm not sure if there's a more efficient way, however here's how I setup the query using DetachedCriteria.
我想你可以使用这个查询:
I think you can use this query: