我需要对客户执行联接。其中地址是主要的并填充 PrimaryAddress1 和 PrimaryCity 字段。客户映射已经与地址类有很多关系,但我不想获取所有地址(性能问题)。
请帮忙..
类:
public class Customer
{
public Customer()
{
Addressess = new List<Address>();
}
public virtual int CustomerID { get; set; }
public virtual int? BranchID { get; set; }
public virtual int? CustTypeID { get; set; }
public virtual string CompanyName { get; set; }
public virtual string Prefix { get; set; }
public virtual string FirstName { get; set; }
public virtual string MiddleName { get; set; }
public virtual string LastName { get; set; }
public virtual string PrimaryAddress1 { get; set; }
public virtual string PrimaryCity { get; set; }
public virtual List<Address> Addresses { get; set; }
}
public class Address
{
public Address()
{
}
public virtual int LocationID { get; set; }
public virtual int? CustomerID { get; set; }
public virtual string LocationName { get; set; }
public virtual string Address1 { get; set; }
public virtual string Address2 { get; set; }
public virtual string Address3 { get; set; }
public virtual string City { get; set; }
public virtual bool Primary { get; set; }
}
映射:
public TblCustomerMap()
{
Table("tblCustomers");
LazyLoad();
Id(x => x.CustomerID).GeneratedBy.Identity().Column("CustomerID");
Map(x => x.ProfileID).Column("ProfileID");
Map(x => x.BranchID).Column("BranchID");
Map(x => x.DateEntered).Column("DateEntered");
Map(x => x.DateTerminated).Column("DateTerminated");
Map(x => x.CustTypeID).Column("CustTypeID");
Map(x => x.CompanyName).Column("CompanyName").Not.Nullable().Length(50);
Map(x => x.Prefix).Column("Prefix").Not.Nullable().Length(50);
Map(x => x.FirstName).Column("FirstName").Not.Nullable().Length(50);
Map(x => x.MiddleName).Column("MiddleName").Not.Nullable().Length(50);
Map(x => x.LastName).Column("LastName").Not.Nullable().Length(50);
HasMany(x => x.Address).KeyColumn("CustomerID");
Map(x => x.PrimaryAddress1).Column("PrimaryAddress1") // from table tbladdress where address is primary and get data from address1 column
Map(x => x.PrimaryCity).Column("PrimaryCity") // from table tbladdress where address is primary and get data from city column
}
询问:
var query = session
.QueryOver<Customer>(() => customer)
.JoinQueryOver(() => customer.Addresses, () => address)
.Where(() => address.Primary)
.List();
foreach (var customer1 in query)
{
customer1.PrimaryAddress1 = customer1.Addresses[0].Address1;
customer1.PrimaryCity = customer1.Addresses[0].City;
customer1.PrimaryState = customer1.Addresses[0].StateOrProvince;
}
新查询:
var query = session.Query<Customer>()
.SelectMany(c => c.Addresses,
(c, a) => new {c, a})
.Where(cust => cust.a.Primary)
.Select(item => new CustomerView()
{
CustomerID = item.c.CustomerID,
CompanyName= item.c.CompanyName,
FirstName= item.c.FirstName,
LastName=item.c.LastName,
Address1=item.a.Address1,
Address2=item.a.Address2,
Address3= item.a.Address3,
City= item.a.City,
StateOrProvince= item.a.StateOrProvince
});
return query.ToList();