LINQ - Sample Queries

This project contains about 101 samples using LINQ.

 
 
 
 
 
4.8 Star
(89)
175,636 times
Add to favorites
3/13/2012
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

VB.NET
C#
<?xml version="1.0" encoding="Windows-1252"?>
<Database Name="Northwind">
  <Table Name="Orders">
    <Type Name="Mapping.Order">
      <Column Name="OrderID" Member="OrderID" Storage="_OrderID" DbType="Int NOT NULL IDENTITY" IsIdentity="True" IsAutoGen="True" />
      <Column Name="CustomerID" Member="CustomerID" Storage="_CustomerID" DbType="NChar(5)" />
      <Column Name="EmployeeID" Member="EmployeeID" Storage="_EmployeeID" DbType="Int" />
      <Column Name="OrderDate" Member="OrderDate" Storage="_OrderDate" DbType="DateTime" />
      <Column Name="RequiredDate" Member="RequiredDate" Storage="_RequiredDate" DbType="DateTime" />
      <Column Name="ShippedDate" Member="ShippedDate" Storage="_ShippedDate" DbType="DateTime" />
      <Column Name="ShipVia" Member="ShipVia" Storage="_ShipVia" DbType="Int" />
      <Column Name="Freight" Member="Freight" Storage="_Freight" DbType="Money" />
      <Column Name="ShipName" Member="ShipName" Storage="_ShipName" DbType="NVarChar(40)" />
      <Column Name="ShipAddress" Member="ShipAddress" Storage="_ShipAddress" DbType="NVarChar(60)" />
      <Column Name="ShipCity" Member="ShipCity" Storage="_ShipCity" DbType="NVarChar(15)" />
      <Column Name="ShipRegion" Member="ShipRegion" Storage="_ShipRegion" DbType="NVarChar(15)" />
      <Column Name="ShipPostalCode" Member="ShipPostalCode" Storage="_ShipPostalCode" DbType="NVarChar(10)" />
      <Column Name="ShipCountry" Member="ShipCountry" Storage="_ShipCountry" DbType="NVarChar(15)" />
      <Association Name="FK_Order_Details_Orders" Member="OrderDetails" Storage="_OrderDetails" ThisKey="OrderID" OtherTable="Order Details" OtherKey="OrderID" />
      <Association Name="FK_Orders_Customers" Member="Customer" Storage="_Customer" ThisKey="CustomerID" OtherTable="Customers" OtherKey="CustomerID" IsParent="True" />
      <Association Name="FK_Orders_Employees" Member="Employee" Storage="_Employee" ThisKey="EmployeeID" OtherTable="Employees" OtherKey="EmployeeID" IsParent="True" />
      <Association Name="FK_Orders_Shippers" Member="Shipper" Storage="_Shipper" ThisKey="ShipVia" OtherTable="Shippers" OtherKey="ShipperID" IsParent="True" />
    </Type>
  </Table>
  <Table Name="Products">
    <Type Name="Mapping.Product">
      <Column Name="ProductID" Member="ProductID" Storage="_ProductID" DbType="Int NOT NULL IDENTITY" IsIdentity="True" IsAutoGen="True" />
      <Column Name="ProductName" Member="ProductName" Storage="_ProductName" DbType="NVarChar(40) NOT NULL" />
      <Column Name="SupplierID" Member="SupplierID" Storage="_SupplierID" DbType="Int" />
      <Column Name="CategoryID" Member="CategoryID" Storage="_CategoryID" DbType="Int" />
      <Column Name="QuantityPerUnit" Member="QuantityPerUnit" Storage="_QuantityPerUnit" DbType="NVarChar(20)" />
      <Column Name="UnitPrice" Member="UnitPrice" Storage="_UnitPrice" DbType="Money" />
      <Column Name="UnitsInStock" Member="UnitsInStock" Storage="_UnitsInStock" DbType="SmallInt" />
      <Column Name="UnitsOnOrder" Member="UnitsOnOrder" Storage="_UnitsOnOrder" DbType="SmallInt" />
      <Column Name="ReorderLevel" Member="ReorderLevel" Storage="_ReorderLevel" DbType="SmallInt" />
      <Column Name="Discontinued" Member="Discontinued" Storage="_Discontinued" DbType="Bit NOT NULL" />
      <Association Name="FK_Order_Details_Products" Member="OrderDetails" Storage="_OrderDetails" ThisKey="ProductID" OtherTable="Order Details" OtherKey="ProductID" />
      <Association Name="FK_Products_Categories" Member="Category" Storage="_Category" ThisKey="CategoryID" OtherTable="Categories" OtherKey="CategoryID" IsParent="True" />
      <Association Name="FK_Products_Suppliers" Member="Supplier" Storage="_Supplier" ThisKey="SupplierID" OtherTable="Suppliers" OtherKey="SupplierID" IsParent="True" />
    </Type>
  </Table>
  <Table Name="Order Details">
    <Type Name="Mapping.OrderDetail">
      <Column Name="OrderID" Member="OrderID" Storage="_OrderID" DbType="Int NOT NULL" IsIdentity="True" />
      <Column Name="ProductID" Member="ProductID" Storage="_ProductID" DbType="Int NOT NULL" IsIdentity="True" />
      <Column Name="UnitPrice" Member="UnitPrice" Storage="_UnitPrice" DbType="Money NOT NULL" />
      <Column Name="Quantity" Member="Quantity" Storage="_Quantity" DbType="SmallInt NOT NULL" />
      <Column Name="Discount" Member="Discount" Storage="_Discount" DbType="Real NOT NULL" />
      <Association Name="FK_Order_Details_Orders" Member="Order" Storage="_Order" ThisKey="OrderID" OtherTable="Orders" OtherKey="OrderID" IsParent="True" />
      <Association Name="FK_Order_Details_Products" Member="Product" Storage="_Product" ThisKey="ProductID" OtherTable="Products" OtherKey="ProductID" IsParent="True" />
    </Type>
  </Table>
  <Table Name="CustomerCustomerDemo">
    <Type Name="Mapping.CustomerCustomerDemo">
      <Column Name="CustomerID" Member="CustomerID" Storage="_CustomerID" DbType="NChar(5) NOT NULL" IsIdentity="True" />
      <Column Name="CustomerTypeID" Member="CustomerTypeID" Storage="_CustomerTypeID" DbType="NChar(10) NOT NULL" IsIdentity="True" />
      <Association Name="FK_CustomerCustomerDemo" Member="CustomerDemographic" Storage="_CustomerDemographic" ThisKey="CustomerTypeID" OtherTable="CustomerDemographics" OtherKey="CustomerTypeID" IsParent="True" />
      <Association Name="FK_CustomerCustomerDemo_Customers" Member="Customer" Storage="_Customer" ThisKey="CustomerID" OtherTable="Customers" OtherKey="CustomerID" IsParent="True" />
    </Type>
  </Table>
  <Table Name="CustomerDemographics">
    <Type Name="Mapping.CustomerDemographic">
      <Column Name="CustomerTypeID" Member="CustomerTypeID" Storage="_CustomerTypeID" DbType="NChar(10) NOT NULL" IsIdentity="True" />
      <Column Name="CustomerDesc" Member="CustomerDesc" Storage="_CustomerDesc" DbType="NText" UpdateCheck="Never" />
      <Association Name="FK_CustomerCustomerDemo" Member="CustomerCustomerDemos" Storage="_CustomerCustomerDemos" ThisKey="CustomerTypeID" OtherTable="CustomerCustomerDemo" OtherKey="CustomerTypeID" />
    </Type>
  </Table>
  <Table Name="Region">
    <Type Name="Mapping.Region">
      <Column Name="RegionID" Member="RegionID" Storage="_RegionID" DbType="Int NOT NULL" IsIdentity="True" />
      <Column Name="RegionDescription" Member="RegionDescription" Storage="_RegionDescription" DbType="NChar(50) NOT NULL" />
      <Association Name="FK_Territories_Region" Member="Territories" Storage="_Territories" ThisKey="RegionID" OtherTable="Territories" OtherKey="RegionID" />
    </Type>
  </Table>
  <Table Name="Territories">
    <Type Name="Mapping.Territory">
      <Column Name="TerritoryID" Member="TerritoryID" Storage="_TerritoryID" DbType="NVarChar(20) NOT NULL" IsIdentity="True" />
      <Column Name="TerritoryDescription" Member="TerritoryDescription" Storage="_TerritoryDescription" DbType="NChar(50) NOT NULL" />
      <Column Name="RegionID" Member="RegionID" Storage="_RegionID" DbType="Int NOT NULL" />
      <Association Name="FK_EmployeeTerritories_Territories" Member="EmployeeTerritories" Storage="_EmployeeTerritories" ThisKey="TerritoryID" OtherTable="EmployeeTerritories" OtherKey="TerritoryID" />
      <Association Name="FK_Territories_Region" Member="Region" Storage="_Region" ThisKey="RegionID" OtherTable="Region" OtherKey="RegionID" IsParent="True" />
    </Type>
  </Table>
  <Table Name="EmployeeTerritories">
    <Type Name="Mapping.EmployeeTerritory">
      <Column Name="EmployeeID" Member="EmployeeID" Storage="_EmployeeID" DbType="Int NOT NULL" IsIdentity="True" />
      <Column Name="TerritoryID" Member="TerritoryID" Storage="_TerritoryID" DbType="NVarChar(20) NOT NULL" IsIdentity="True" />
      <Association Name="FK_EmployeeTerritories_Employees" Member="Employee" Storage="_Employee" ThisKey="EmployeeID" OtherTable="Employees" OtherKey="EmployeeID" IsParent="True" />
      <Association Name="FK_EmployeeTerritories_Territories" Member="Territory" Storage="_Territory" ThisKey="TerritoryID" OtherTable="Territories" OtherKey="TerritoryID" IsParent="True" />
    </Type>
  </Table>
  <Table Name="Employees">
    <Type Name="Mapping.Employee">
      <Column Name="EmployeeID" Member="EmployeeID" Storage="_EmployeeID" DbType="Int NOT NULL IDENTITY" IsIdentity="True" IsAutoGen="True" />
      <Column Name="LastName" Member="LastName" Storage="_LastName" DbType="NVarChar(20) NOT NULL" />
      <Column Name="FirstName" Member="FirstName" Storage="_FirstName" DbType="NVarChar(10) NOT NULL" />
      <Column Name="Title" Member="Title" Storage="_Title" DbType="NVarChar(30)" />
      <Column Name="TitleOfCourtesy" Member="TitleOfCourtesy" Storage="_TitleOfCourtesy" DbType="NVarChar(25)" />
      <Column Name="BirthDate" Member="BirthDate" Storage="_BirthDate" DbType="DateTime" />
      <Column Name="HireDate" Member="HireDate" Storage="_HireDate" DbType="DateTime" />
      <Column Name="Address" Member="Address" Storage="_Address" DbType="NVarChar(60)" />
      <Column Name="City" Member="City" Storage="_City" DbType="NVarChar(15)" />
      <Column Name="Region" Member="Region" Storage="_Region" DbType="NVarChar(15)" />
      <Column Name="PostalCode" Member="PostalCode" Storage="_PostalCode" DbType="NVarChar(10)" />
      <Column Name="Country" Member="Country" Storage="_Country" DbType="NVarChar(15)" />
      <Column Name="HomePhone" Member="HomePhone" Storage="_HomePhone" DbType="NVarChar(24)" />
      <Column Name="Extension" Member="Extension" Storage="_Extension" DbType="NVarChar(4)" />
      <Column Name="Photo" Member="Photo" Storage="_Photo" DbType="Image" UpdateCheck="Never" />
      <Column Name="Notes" Member="Notes" Storage="_Notes" DbType="NText" UpdateCheck="Never" />
      <Column Name="ReportsTo" Member="ReportsTo" Storage="_ReportsTo" DbType="Int" />
      <Column Name="PhotoPath" Member="PhotoPath" Storage="_PhotoPath" DbType="NVarChar(255)" />
      <Association Name="FK_Employees_Employees" Member="ReportsToEmployee" Storage="_ReportsToEmployee" ThisKey="ReportsTo" OtherTable="Employees" OtherKey="EmployeeID" IsParent="True" />
      <Association Name="FK_Employees_Employees" Member="Employees" Storage="_Employees" ThisKey="EmployeeID" OtherTable="Employees" OtherKey="ReportsTo" />
      <Association Name="FK_EmployeeTerritories_Employees" Member="EmployeeTerritories" Storage="_EmployeeTerritories" ThisKey="EmployeeID" OtherTable="EmployeeTerritories" OtherKey="EmployeeID" />
      <Association Name="FK_Orders_Employees" Member="Orders" Storage="_Orders" ThisKey="EmployeeID" OtherTable="Orders" OtherKey="EmployeeID" />
    </Type>
  </Table>
  <Table Name="Categories">
    <Type Name="Mapping.Category">
      <Column Name="CategoryID" Member="CategoryID" Storage="_CategoryID" DbType="Int NOT NULL IDENTITY" IsIdentity="True" IsAutoGen="True" />
      <Column Name="CategoryName" Member="CategoryName" Storage="_CategoryName" DbType="NVarChar(15) NOT NULL" />
      <Column Name="Description" Member="Description" Storage="_Description" DbType="NText" UpdateCheck="Never" />
      <Column Name="Picture" Member="Picture" Storage="_Picture" DbType="Image" UpdateCheck="Never" />
      <Association Name="FK_Products_Categories" Member="Products" Storage="_Products" ThisKey="CategoryID" OtherTable="Products" OtherKey="CategoryID" />
    </Type>
  </Table>
  <Table Name="Customers">
    <Type Name="Mapping.Customer">
      <Column Name="CustomerID" Member="CustomerID" Storage="_CustomerID" DbType="NChar(5) NOT NULL" IsIdentity="True" />
      <Column Name="CompanyName" Member="CompanyName" Storage="_CompanyName" DbType="NVarChar(40) NOT NULL" />
      <Column Name="ContactName" Member="ContactName" Storage="_ContactName" DbType="NVarChar(30)" />
      <Column Name="ContactTitle" Member="ContactTitle" Storage="_ContactTitle" DbType="NVarChar(30)" />
      <Column Name="Address" Member="Address" Storage="_Address" DbType="NVarChar(60)" />
      <Column Name="City" Member="City" Storage="_City" DbType="NVarChar(15)" />
      <Column Name="Region" Member="Region" Storage="_Region" DbType="NVarChar(15)" />
      <Column Name="PostalCode" Member="PostalCode" Storage="_PostalCode" DbType="NVarChar(10)" />
      <Column Name="Country" Member="Country" Storage="_Country" DbType="NVarChar(15)" />
      <Column Name="Phone" Member="Phone" Storage="_Phone" DbType="NVarChar(24)" />
      <Column Name="Fax" Member="Fax" Storage="_Fax" DbType="NVarChar(24)" />
      <Association Name="FK_CustomerCustomerDemo_Customers" Member="CustomerCustomerDemos" Storage="_CustomerCustomerDemos" ThisKey="CustomerID" OtherTable="CustomerCustomerDemo" OtherKey="CustomerID" />
      <Association Name="FK_Orders_Customers" Member="Orders" Storage="_Orders" ThisKey="CustomerID" OtherTable="Orders" OtherKey="CustomerID" />
    </Type>
  </Table>
  <Table Name="Shippers">
    <Type Name="Mapping.Shipper">
      <Column Name="ShipperID" Member="ShipperID" Storage="_ShipperID" DbType="Int NOT NULL IDENTITY" IsIdentity="True" IsAutoGen="True" />
      <Column Name="CompanyName" Member="CompanyName" Storage="_CompanyName" DbType="NVarChar(40) NOT NULL" />
      <Column Name="Phone" Member="Phone" Storage="_Phone" DbType="NVarChar(24)" />
      <Association Name="FK_Orders_Shippers" Member="Orders" Storage="_Orders" ThisKey="ShipperID" OtherTable="Orders" OtherKey="ShipVia" />
    </Type>
  </Table>
  <Table Name="Suppliers">
    <Type Name="Mapping.Supplier">
      <Column Name="SupplierID" Member="SupplierID" Storage="_SupplierID" DbType="Int NOT NULL IDENTITY" IsIdentity="True" IsAutoGen="True" />
      <Column Name="CompanyName" Member="CompanyName" Storage="_CompanyName" DbType="NVarChar(40) NOT NULL" />
      <Column Name="ContactName" Member="ContactName" Storage="_ContactName" DbType="NVarChar(30)" />
      <Column Name="ContactTitle" Member="ContactTitle" Storage="_ContactTitle" DbType="NVarChar(30)" />
      <Column Name="Address" Member="Address" Storage="_Address" DbType="NVarChar(60)" />
      <Column Name="City" Member="City" Storage="_City" DbType="NVarChar(15)" />
      <Column Name="Region" Member="Region" Storage="_Region" DbType="NVarChar(15)" />
      <Column Name="PostalCode" Member="PostalCode" Storage="_PostalCode" DbType="NVarChar(10)" />
      <Column Name="Country" Member="Country" Storage="_Country" DbType="NVarChar(15)" />
      <Column Name="Phone" Member="Phone" Storage="_Phone" DbType="NVarChar(24)" />
      <Column Name="Fax" Member="Fax" Storage="_Fax" DbType="NVarChar(24)" />
      <Column Name="HomePage" Member="HomePage" Storage="_HomePage" DbType="NText" UpdateCheck="Never" />
      <Association Name="FK_Products_Suppliers" Member="Products" Storage="_Products" ThisKey="SupplierID" OtherTable="Products" OtherKey="SupplierID" />
    </Type>
  </Table>
  <Table Name="Contacts">
    <Type Name="Mapping.BaseContact" InheritanceCode="Unknown" IsInheritanceDefault="True">
      <Column Name="ContactID" Member="ContactID" IsIdentity="True" IsAutoGen="True" />
      <Column Name="CompanyName" Member="CompanyName" DbType="NVARCHAR(40)" />
      <Column Name="Phone" Member="Phone" DbType="NVarChar(24)" />
      <Column Name="ContactType" Member="ContactType" DbType="NVarChar(50) NULL" IsDiscriminator="True" />
      <Type Name="Mapping.ShipperContact" InheritanceCode="Shipper">
      </Type>
      <Type Name="Mapping.FullContact">
        <Column Name="ContactName" Member="ContactName" DbType="NVarChar(40)" />
        <Column Name="ContactTitle" Member="ContactTitle" DbType="NVarChar(30)" />
        <Column Name="Address" Member="Address" DbType="NVarChar(60)" />
        <Column Name="City" Member="City" DbType="NVarChar(15)" />
        <Column Name="Region" Member="Region" DbType="NVarChar(15)" />
        <Column Name="PostalCode" Member="PostalCode" DbType="NVarChar(10)" />
        <Column Name="Country" Member="Country" DbType="NVarChar(15)" />
        <Column Name="Fax" Member="Fax" DbType="NVarChar(24)" />
        <Type Name="Mapping.SupplierContact" InheritanceCode="Supplier">
          <Column Name="HomePage" Member="HomePage" DbType="NTEXT" />
        </Type>
        <Type Name="Mapping.CustomerContact" InheritanceCode="Customer">
        </Type>
        <Type Name="Mapping.EmployeeContact" InheritanceCode="Employee">
          <Column Name="Extension" Member="Extension" DbType="NVarChar(4)" />
          <Column Name="PhotoPath" Member="PhotoPath" DbType="NVarChar(255)" />
          <Column Name="Image" Member="Image" DbType="Image" />
        </Type>
      </Type>
    </Type>
  </Table>
  <StoredProcedure Name="CustOrderHist" Method="CustomerOrderHistory">
    <Parameter Name="CustomerID" Parameter="customerID" DbType="NChar(5)"/>
    <Type Name="CustOrderHistResult">
      <Column Name="ProductName" Member="ProductName" Storage="_ProductName" DbType="NVarChar(40)"/>
      <Column Name="Total" Member="Total" Storage="_Total" DbType="Int" />
    </Type>
  </StoredProcedure>
  <TableValuedFunction Name="ProductsUnderThisUnitPrice" Method="ProductsUnderThisUnitPrice">
    <Parameter Name="param" Parameter="price" DbType="Money" />
    <Type Name="Mapping.Product">
      <Column Name="ProductID" Member="ProductID" Storage="_ProductID" DbType="Int NOT NULL IDENTITY" IsIdentity="True" IsAutoGen="True" />
      <Column Name="ProductName" Member="ProductName" Storage="_ProductName" DbType="NVarChar(40) NOT NULL" />
      <Column Name="SupplierID" Member="SupplierID" Storage="_SupplierID" DbType="Int" />
      <Column Name="CategoryID" Member="CategoryID" Storage="_CategoryID" DbType="Int" />
      <Column Name="QuantityPerUnit" Member="QuantityPerUnit" Storage="_QuantityPerUnit" DbType="NVarChar(20)" />
      <Column Name="UnitPrice" Member="UnitPrice" Storage="_UnitPrice" DbType="Money" />
      <Column Name="UnitsInStock" Member="UnitsInStock" Storage="_UnitsInStock" DbType="SmallInt" />
      <Column Name="UnitsOnOrder" Member="UnitsOnOrder" Storage="_UnitsOnOrder" DbType="SmallInt" />
      <Column Name="ReorderLevel" Member="ReorderLevel" Storage="_ReorderLevel" DbType="SmallInt" />
      <Column Name="Discontinued" Member="Discontinued" Storage="_Discontinued" DbType="Bit NOT NULL" />
      <Association Name="FK_Order_Details_Products" Member="OrderDetails" Storage="_OrderDetails" ThisKey="ProductID" OtherTable="Order Details" OtherKey="ProductID" />
      <Association Name="FK_Products_Categories" Member="Category" Storage="_Category" ThisKey="CategoryID" OtherTable="Categories" OtherKey="CategoryID" IsParent="True" />
      <Association Name="FK_Products_Suppliers" Member="Supplier" Storage="_Supplier" ThisKey="SupplierID" OtherTable="Suppliers" OtherKey="SupplierID" IsParent="True" />
    </Type>
  </TableValuedFunction>
  <UserDefinedFunction Name="dbo.TotalProductUnitPriceByCategory" Method="TotalProductUnitPriceByCategory">
    <Parameter Name="categoryID" Parameter="categoryID" DbType="Int"/>
  </UserDefinedFunction>
</Database>