fma3
fma3

Reputation: 571

Querying two tables, from code

I have two tables.

the first table: Employee

IdEmployee
Name
LastName

The second table: CommentsEmployee

IdComment
IdEmployee
Note

make consultation with the IdEmployee in the two tables, get Name, IdComment, and Note from C# Tables are of type SQL was trying

DetachedCriteria criteria1 = DetachedCriteria.For<Employee>()
                                .Add(Restrictions.Eq("IdEmployee", 2))
                                .SetFetchMode("Name", FetchMode.Eager);

                DetachedCriteria criteria2 = DetachedCriteria.For<CommentsEmployee>()
                                .Add(Restrictions.Eq("IdEmployee", 2))
                                .SetFetchMode("IdComment", FetchMode.Eager)
                                .SetFetchMode("Note", FetchMode.Eager);

                var result = session.CreateMultiCriteria()
                    .Add(criteria1)
                    .Add(criteria2)
                    .List();

/////////////////////////////////////////////////////////////////////////////////////// otherwise

var result = session
                        .CreateCriteria<Employee>()
                        .CreateCriteria("CommentsEmployee")
                        .Add(Restrictions.Eq("IdEmployee", f))
                        .List();

anyone can help me

Upvotes: 0

Views: 305

Answers (2)

Peter
Peter

Reputation: 27944

I think you should have a look at bag. Add the comments as a bag to employee and you only need 1 get/load statement.

The Employee class:

IdEmployee
Name
LastName
IList <CommentsEmployee> Comments

The xml for the bag:

<bag name="Comments" inverse="false" lazy="true" cascade="save-update" generic="true">
    <key column="EmployeeId"/>
    <one-to-many class="EmployeeComments">
</bag>

Upvotes: 0

Task
Task

Reputation: 3686

I think you're looking for a simple join statement?

SELECT * FROM Employee JOIN CommentsEmployee ON Employee.IdEmployee = CommentsEmployee.IdEmployee;

That's generic SQL, I hope it works for you but at least it should convey the concept.

Upvotes: 2

Related Questions