BLToolkit Association Linq
Here is an example usage of list associations taken from BLToolkit http://bltoolkit.net/(S(ibvuiu3itvirtq550l4r0n55))/Doc.LinqAssociations.ashx
[TableName("Categories")]
public class Category
{
[PrimaryKey, Identity] public int CategoryID;
[NotNull] public string CategoryName;
public string Description;
public Binary Picture;
[Association(ThisKey="CategoryID", OtherKey="CategoryID")]
public List<Product> Products;
}
AND
[TableName("Products")]
public abstract class Product
{
[PrimaryKey, Identity] public int ProductID;
[NotNull] public string ProductName;
public int? SupplierID;
public int? CategoryID;
public string QuantityPerUnit;
public decimal? UnitPrice;
public short? UnitsInStock;
public short? UnitsOnOrder;
public short? ReorderLevel;
[MapField(IsInheritanceDis开发者_如何学运维criminator=true)] public bool Discontinued;
[Association(ThisKey="ProductID", OtherKey="ProductID")]
public List<OrderDetail> OrderDetails;
[Association(ThisKey="CategoryID", OtherKey="CategoryID", CanBeNull=false)]
public Category Category;
[Association(ThisKey="SupplierID", OtherKey="SupplierID", CanBeNull=false)]
public Supplier Supplier;
}
When I try
using (var db = new NorthwindDB())
{
var query =
from p in db.Product
select new
{
p.Category.CategoryName,
p.ProductName
};
foreach (var item in query)
{
Console.WriteLine(item);
}
}
Returns with product CategoryName
BUT WHEN I TRY THIS WAY (like linq2Sql)
var db1 = new NorthwindDB();
db1.Product.First().Category.CategoryName;
Category returns null????
Try this
var db1 = new NorthwindDB();
db1.Product.Select(prod => prod.Category.CategoryName).First();
I Think this does the same. Anyway about the "like linq2Sql", L2S has lazy loading, Blt doesn't
精彩评论