Nhibernate build criteria

I was wondering if anyone could help with an example criteria for nhibernate. I think I have a head around quite simple things, such as finding records in a given table, where the field corresponds to a specific value, etc. Where I am stumbling right now, you have a table with a foreign key for another table and trying to find rows from the first table based on some field in the second table.

As an example...

If i have

tblUser
pk int    ID
   int    CompanyID
   string Name

and

tblCompany
pk int     ID
   string CompanyName

How to create criteria for extracting all users belonging to a company with a specific name? I think that I understand how to create the appropriate files / objects for matching, I just can’t understand how to make the criteria look at the subtasks of the original object.

Any examples can be amazing.

+3
1

-, , -,

session.CreateCriteria<User>()
    .CreateAlias("Company", "c") //the first argument is the property name from User
    .Add(Restrictions.Eq("c.Name", companyName))
    .List<User>();

, , , User β†’ Company β†’ Owner,

.CreateAlias("Company", "c")
.CreateAlias("c.Owner", "o")

, , .

session.CreateCriteria<User>("u")
    .CreateAlias("u.Company", "c")
    .Add(Restrictions.Eq("c.Name", companyName))
    .List<User>();
+10

Source: https://habr.com/ru/post/1716925/


All Articles