Nhibernate和Query示例

时间:2016-03-01 16:43:35

标签: c# entity-framework linq nhibernate hql

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>();

这个返回显示在图片中的新对象    [但我想以客户身份访问此对象,并命令如何获取数据&#39; customerObject.propertyName和order.propertyName&#34;而不是用字符串获取数组。我可以使用客户和订单对象这样的两个对象获取数据列表,以便我可以使用该对象访问这些数据] 它是returns aa[0] = {name=" sam ", product=" Book"},但我需要aa[0] = {Customer.Name , Order.product }之类的内容 [1]

1 个答案:

答案 0 :(得分:1)

您可以尝试返回映射的对象。考虑到你有映射的实体,尝试这样的事情:

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

然后,您可以将result对象作为匿名对象的集合访问,其中您拥有Customer属性,Product具有您的实体。