您的位置:知识库 »

LINQ to SQL语句(25)之继承

作者: 李永京  来源: 博客园  发布时间: 2008-09-18 18:43  阅读: 8489 次  推荐: 0   原文链接   [收藏]  
摘要:这个系列的最后一篇,讲解继承,我们要衷心地感谢博客园和作者提供了这么完整的参考资料。
[1] 继承基础
[2] 继承实例

系列文章导航:

LINQ to SQL语句(1)之Where

LINQ to SQL语句(2)之Select/Distinct

LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg

LINQ to SQL语句(4)之Join

LINQ to SQL语句(5)之Order By

LINQ to SQL语句(6)之Group By/Having

LINQ to SQL语句(7)之Exists/In/Any/All/Contains

LINQ to SQL语句(8)之Concat/Union/Intersect/Except

LINQ to SQL语句(9)之Top/Bottom和Paging和SqlMethods

LINQ to SQL语句(10)之Insert

LINQ to SQL语句(11)之Update

LINQ to SQL语句(12)之Delete和使用Attach

LINQ to SQL语句(13)之开放式并发控制和事务

LINQ to SQL语句(14)之Null语义和DateTime

LINQ to SQL语句(15)之String

LINQ to SQL语句(16)之对象标识

LINQ to SQL语句(17)之对象加载

LINQ to SQL语句(18)之运算符转换

LINQ to SQL语句(19)之ADO.NET与LINQ to SQL

LINQ to SQL语句(20)之存储过程

LINQ to SQL语句(21)之用户定义函数

LINQ to SQL语句(22)之DataContext

LINQ to SQL语句(23)之动态查询

LINQ to SQL语句(24)之视图

LINQ to SQL语句(25)之继承

LINQ简介

adsfsaf


1.一般形式

日常我们经常写的形式,对单表查询。

var cons = from c in db.Contacts                       
           select c;
foreach (var con in cons) {
    Console.WriteLine("Company name: {0}", con.CompanyName);
    Console.WriteLine("Phone: {0}", con.Phone);
    Console.WriteLine("This is a {0}", con.GetType());
}

2.OfType形式

这里我仅仅让其返回顾客的联系方式。

var cons = from c in db.Contacts.OfType<CustomerContact>()
           select c;

初步学习,我们还是看看生成的SQL语句,这样容易理解。在SQL语句中查询了ContactType为Customer的联系方式。

SELECT [t0].[ContactType], [t0].[ContactName], [t0].[ContactTitle],
[t0].[Address],[t0].[City], [t0].[Region], [t0].[PostalCode], 
[t0].[Country], [t0].[Fax],[t0].[ContactID], [t0].[CompanyName], 
[t0].[Phone] FROM [dbo].[Contacts] AS [t0]
WHERE ([t0].[ContactType] = @p0) AND ([t0].[ContactType] IS NOT NULL)
-- @p0: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Customer]

3.IS形式

这个例子查找一下发货人的联系方式。

var cons = from c in db.Contacts
           where c is ShipperContact
           select c;

生成的SQL语句如下:查询了ContactType为Shipper的联系方式。大致一看好像很上面的一样,其实这里查询出来的列多了很多。实际上是Contacts表的全部字段。

SELECT [t0].[ContactType], [t0].[ContactID], [t0].[CompanyName],
[t0].[Phone],[t0].[HomePage], [t0].[ContactName],
[t0].[ContactTitle], [t0].[Address], [t0].[City],
[t0].[Region], [t0].[PostalCode], [t0].[Country],
[t0].[Fax],[t0].[PhotoPath], [t0].[Photo], [t0].[Extension]
FROM [dbo].[Contacts] AS [t0] WHERE ([t0].[ContactType] = @p0)
AND ([t0].[ContactType] IS NOT NULL)
-- @p0: Input NVarChar (Size = 7; Prec = 0; Scale = 0) [Shipper]

4.AS形式

这个例子就通吃了,全部查找了一番。

var cons = from c in db.Contacts
           select c as FullContact;

生成SQL语句如下:查询整个Contacts表。

SELECT [t0].[ContactType], [t0].[HomePage], [t0].[ContactName],
[t0].[ContactTitle],[t0].[Address], [t0].[City],
[t0].[Region], [t0].[PostalCode], [t0].[Country], 
[t0].[Fax], [t0].[ContactID], [t0].[CompanyName], 
[t0].[Phone], [t0].[PhotoPath],[t0].[Photo], [t0].[Extension]
 FROM [dbo].[Contacts] AS [t0]

5.Cast形式

使用Case形式查找出在伦敦的顾客的联系方式。

var cons = from c in db.Contacts
           where c.ContactType == "Customer" && 
                     ((CustomerContact)c).City == "London"
           select c;

生成SQL语句如下,自己可以看懂了。

SELECT [t0].[ContactType], [t0].[ContactID], [t0].[CompanyName],
[t0].[Phone], [t0].[HomePage],[t0].[ContactName],
[t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region],
[t0].[PostalCode], [t0].[Country], [t0].[Fax], [t0].[PhotoPath],
[t0].[Photo], [t0].[Extension]FROM [dbo].[Contacts] AS [t0]
WHERE ([t0].[ContactType] = @p0) AND ([t0].[City] = @p1)
-- @p0: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Customer]
-- @p1: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]

6.UseAsDefault形式

当插入一条记录时,使用默认的映射关系了,但是在查询时,使用继承的关系了。具体看看生成的SQL语句就直截了当了。

//插入一条数据默认使用正常的映射关系
Contact contact = new Contact()
{
    ContactType = null,
    CompanyName = "Unknown Company",
    Phone = "333-444-5555"
};
db.Contacts.InsertOnSubmit(contact);
db.SubmitChanges();
//查询一条数据默认使用继承映射关系
var con =
    (from c in db.Contacts
     where c.CompanyName == "Unknown Company" &&
                            c.Phone == "333-444-5555"
     select c).First();

生成SQL语句如下:

INSERT INTO [dbo].[Contacts]([ContactType], [CompanyName],
[Phone]) VALUES (@p0, @p1, @p2)

SELECT TOP (1) [t0].[ContactType], [t0].[ContactID],
[t0].[CompanyName], [t0].[Phone],[t0].[HomePage],
[t0].[ContactName], [t0].[ContactTitle], [t0].[Address],
[t0].[City],[t0].[Region], [t0].[PostalCode], [t0].[Country],
[t0].[Fax], [t0].[PhotoPath],[t0].[Photo], [t0].[Extension]
FROM [dbo].[Contacts] AS [t0]
WHERE ([t0].[CompanyName] = @p0) AND ([t0].[Phone] = @p1)
-- @p0: Input NVarChar (Size = 15; Prec = 0; Scale = 0)
    [Unknown Company]
-- @p1: Input NVarChar (Size = 12; Prec = 0; Scale = 0)
    [333-444-5555]

7.插入新的记录

这个例子说明如何插入发货人的联系方式的一条记录。

//1.在插入之前查询一下,没有数据
var ShipperContacts =
    from sc in db.Contacts.OfType<ShipperContact>()
    where sc.CompanyName == "Northwind Shipper"
    select sc;
//2.插入数据
ShipperContact nsc = new ShipperContact()
{
    CompanyName = "Northwind Shipper",
    Phone = "(123)-456-7890"
};
db.Contacts.InsertOnSubmit(nsc);
db.SubmitChanges();
//3.查询数据,有一条记录
ShipperContacts =
    from sc in db.Contacts.OfType<ShipperContact>()
    where sc.CompanyName == "Northwind Shipper"
    select sc;
//4.删除记录
db.Contacts.DeleteOnSubmit(nsc);
db.SubmitChanges();

生成SQL语句如下:

SELECT COUNT(*) AS [value] FROM [dbo].[Contacts] AS [t0]
WHERE ([t0].[CompanyName] = @p0) AND ([t0].[ContactType] = @p1) 
AND ([t0].[ContactType] IS NOT NULL)
-- @p0: Input NVarChar [Northwind Shipper]
-- @p1: Input NVarChar [Shipper]

INSERT INTO [dbo].[Contacts]([ContactType], [CompanyName], [Phone])
VALUES (@p0, @p1, @p2)
-- @p0: Input NVarChar  [Shipper]
-- @p1: Input NVarChar  [Northwind Shipper]
-- @p2: Input NVarChar  [(123)-456-7890]

SELECT COUNT(*) AS [value] FROM [dbo].[Contacts] AS [t0]
WHERE ([t0].[CompanyName] = @p0) AND ([t0].[ContactType] = @p1)
AND ([t0].[ContactType] IS NOT NULL)
-- @p0: Input NVarChar [Northwind Shipper]
-- @p1: Input NVarChar [Shipper]

DELETE FROM [dbo].[Contacts] WHERE ([ContactID] = @p0) AND
([ContactType] = @p1) AND ([CompanyName] = @p2) AND ([Phone] = @p3)
-- @p0: Input Int  [159]
-- @p1: Input NVarChar  [Shipper]
-- @p2: Input NVarChar  [Northwind Shipper]
-- @p3: Input NVarChar  [(123)-456-7890]
-- @p4: Input NVarChar  [Unknown]
-- @p5: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Supplier]
-- @p6: Input NVarChar (Size = 7; Prec = 0; Scale = 0) [Shipper]
-- @p7: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Employee]
-- @p8: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Customer]
[第1页][第2页]
0
1
标签:LINQ LINQ to SQL

热门文章

    最新文章

      最新新闻

        热门新闻