且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将sql union转换为linq

更新时间:2023-02-14 10:21:34

对集合进行操作的三个有用的Linq概念.给定集合c和集合e:

Three useful Linq concepts operating on sets. Given set c and set e:

Concat为您提供ce中的所有内容:

Concat gives you everything in c or e:

(From c In db.Customers Select c.Phone).Concat( _
             From c In db.Customers Select c.Fax).Concat( _
             From e In db.Employees Select e.HomePhone)

(From c In db.Customers _
            Select Name = c.CompanyName, Phone = c.Phone).Concat(From e In db.Employees _
            Select Name = e.FirstName & " " & e.LastName, Phone = e.HomePhone)

联盟还为您提供ce中的所有内容,但会删除所有重复项:

Union also gives you everything in c and e, but removes any duplicates:

(From c In db.Customers _
        Select c.Country).Union(From e In db.Employees _
        Select e.Country)

除了在c中为您提供的所有内容以外,在e中没有其他内容:

Except gives you everything in c that is not in e:

(From c In db.Customers _
             Select c.Country).Except(From e In db.Employees Select e.Country)