BLToolkit 协会 Linq

发布于 2024-11-29 21:02:44 字数 2580 浏览 0 评论 0原文

以下是取自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2024-12-06 21:02:44

试试这个

var db1 = new NorthwindDB(); 
db1.Product.Select(prod => prod.Category.CategoryName).First();

我想这也是一样的。
不管怎样,关于“像 linq2Sql”,L2S 有延迟加载,Blt 没有

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文