当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Cast的妙用:泛用LINQ 語句

ASP.NET
Asp.Net 通用数据操作类 (附通用数据基类)
asp.net汉字转拼音和获取汉字首字母的代码
asp.net 多字段模糊查询代码
OpenCms 带分页的新闻列表
URLRewriter最简单入门介绍 URLRewriter相关资源
asp.net Repeater取得CheckBox选中的某行某个值
asp.net清空Cookie的两种方法
asp.net一些很酷很实用的.Net技巧
asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式
asp.net TripleDES加密、解密算法
Asp.net中防止用户多次登录的方法
asp.net Repeater取得CheckBox选中的某行某个值的c#写法
asp.net DataGridView导出到Excel的三个方法[亲测]
C#,winform,ShowDialog,子窗体向父窗体传值
asp.net学习中发现的比较完整的流程
ASP.net 页面被关闭后,服务器端是否仍然执行中?
asp.net Context.Handler 页面间传值方法
asp.net xml序列化与反序列化
asp.net实例代码protected override void Render(HtmlTextWriter writer)
asp.net(c#)捕捉搜索引擎蜘蛛和机器人

ASP.NET 中的 Cast的妙用:泛用LINQ 語句


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 178 ::
收藏到网摘: n/a

什么是泛用LINQ 语句

泛用LINQ语句的概念很简单,在一些情况下,我们会有需求使用同一段程式码来对不同资料表做查询,这在ADO.NET中很容易达到,见下例:

此函式接受一个Reader物件,然后顷印CustomerID栏位值,这不受限于SqlDataReader所选取的Schema或是资料表,只要Schema中有CustomerID栏位即可。

不过,这样的手法在LINQ这种Typed-Query(具型别查询语句)模式下,并没有很直觉的写法,因为你不能写下下面的句子。

以下为引用的内容:
static void Test2(int index)
{
      var query;
      DataClasses1DataContext context = new DataClasses1DataContext();
      context.Log = Console.Out; //for log only,you can remove it.
      if (index == 1)
          query = context.Customers;
      else
          query = context.Orders;
 
      var result = from s1 in query where s1.CustomerID.Contains("V")
                   select s1;
      foreach (var item in result)
      {
         Console.WriteLine(item.CustomerID);
      }
}

编译器会抱怨,var的变数必须在宣告时指定。那要如何在LINQ To SQL或是LINQ To Entites达到同样的效果呢?这有几个方法可以做到。

1、使用ExecuteQuery,并使用另一个Typed物件来接收回传集。

2、使用实体类别(Entity Class)继承。

3、使用Cast与partial class。

1与2对熟悉LINQ To SQL的读者应该不难,所以我就不再赘述了,第三个手法是较少见的,我们可以运用partial class机制,让Entity Classes实作特定介面,然后以Cast函式来达到目的。

以下为引用的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.SqlClient;
 
namespace ConsoleApplication39
{
    class Program
    {
        static void Main(string[] args)
        {
            Test(1);
            Console.WriteLine("------");
            Test(0);
            Console.ReadLine();
        }
 
        static void Test(int index)
        {
            IQueryable<IGenericBaseClass> query = null;
            DataClasses1DataContext context = new DataClasses1DataContext();
            context.Log = Console.Out; //for log only,you can remove it.
            if(index == 1)
                query = context.Customers.Cast<IGenericBaseClass>();
            else
                query = context.Orders.Cast<IGenericBaseClass>();
 
            var result = from s1 in query where s1.CustomerID.Contains("V") select s1;
            foreach (var item in result)
            {
                Console.WriteLine(item.CustomerID);
            }
        }
    }
 
    public interface IGenericBaseClass
    {
        string CustomerID { get; set; }
    }
 
    partial class Customers : IGenericBaseClass
    {
    }
 
    partial class Orders : IGenericBaseClass
    {
    }
}

仔细揣摩上面的代码,我相信你会找到一个不一样的LINQ应用手法。