zoukankan      html  css  js  c++  java
  • 光脚丫学LINQ(018):创建并使用自定义的数据上下文

    视频演示:http://u.115.com/file/f233b821ab

    从数据库的强类型化视图着手要容易得多。 通过将 DataContext 对象强类型化,您无需调用 GetTable。 当您使用强类型化的 DataContext 对象时,您可以在所有查询中使用强类型化表。
    在以下步骤中,您将创建 Customers 作为映射到数据库中的 Customers 表的强类型化表。


    对DataContext对象进行强类型化

    public class Northwind : DataContext   
    {   
        // Table<T> abstracts database details per table/data type.   
        public Table<Customer> Customers;   
        public Table<Order> Orders;   
      
        public Northwind(string connection) : base(connection) { }   
    }  
    public class Northwind : DataContext
    {
        // Table<T> abstracts database details per table/data type.
        public Table<Customer> Customers;
        public Table<Order> Orders;
    
        public Northwind(string connection) : base(connection) { }
    }
    


    然后就可以使用下面的方式来使用自定义的强类型数据上下文了,从而代替系统自带的DataContext。

    // Use a connection string.   
    Northwind db = new Northwind(@"C:\linqtest5\Northwind.mdf");   
      
    // Query for customers from Seattle.    
    var SeattleCustomers =   
        from CustomerObject in db.Customers   
        where CustomerObject.City == "Seattle"  
        select CustomerObject;   
      
    foreach (var CustomerObject in SeattleCustomers)   
    {   
        Console.WriteLine("ID={0}", CustomerObject.CustomerID);   
    }   
      
    // Freeze the console window.   
    Console.ReadLine(); 
    
  • 相关阅读:
    使用一行Python代码从图像读取文本
    FastAI 简介
    OpenCV-Python 轮廓:更多属性 | 二十四
    Numpy和OpenCV中的图像几何变换
    从云计算到边缘计算
    OpenCV-Python 轮廓属性 | 二十三
    c# GetType()和typeof()的区别
    C# 子类父类方法同名,三种处理方式
    C# string 与 String的区别
    decimal
  • 原文地址:https://www.cnblogs.com/GJYSK/p/1865026.html
Copyright © 2011-2022 走看看