Search code examples
c#entity-frameworklinqnhibernatehql

Nhibernate and Query example


class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class Order
{
    public int ID { get; set; }
    public string Product { get; set; }
}

var customers = new Customer[]
{
    new Customer{ID = 5, Name = "Sam"},
    new Customer{ID = 6, Name = "Dave"},
    new Customer{ID = 7, Name = "Julia"},
    new Customer{ID = 8, Name = "Sue"}
};

var orders = new Order[]
{
    new Order{ID = 5, Product = "Book"},
    new Order{ID = 6, Product = "Game"},
    new Order{ID = 7, Product = "Computer"},
    new Order{ID = 8, Product = "Shirt"}
};

IEnumerable<Object> query = 
    from c in customers
    join o in orders on c.ID equals o.ID
    select new { c.Name, o.Product };
IList<Object> AA = query.ToList<Object>();

This one return new object that is shown into picture [But I want to access this object as customer and order how can I get data as 'customerObject.propertyName and order.propertyName" instead of getting array with string. Can I get list of data with two object like customer and order object so I can access those data using that object] It returns aa[0] = {name=" sam ", product=" Book"} but I want something like aa[0] = {Customer.Name , Order.product } [1]


Solution

  • You could try to return the mapped objects. Considering you have your mapped entities, try something like this:

    var result = (from c in customers
                 join o in orders on c.ID equals o.ID
                 select new{ Customer = c, Product = o.Product })
                 .ToList();
    

    Then you can access you result object as a collection of anonymous objects where you have the Customer property and Product which has your entities.