BLToolkit 协会 Linq
以下是取自 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;
}
并且
[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(IsInheritanceDiscriminator=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;
}
当我尝试
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);
}
}
使用产品 CategoryName 返回
时但当我尝试这种方式时(如 linq2Sql)
var db1 = new NorthwindDB();
db1.Product.First().Category.CategoryName;
类别返回无效的????
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(IsInheritanceDiscriminator=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????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
我想这也是一样的。
不管怎样,关于“像 linq2Sql”,L2S 有延迟加载,Blt 没有
Try this
I Think this does the same.
Anyway about the "like linq2Sql", L2S has lazy loading, Blt doesn't