Yes of course. In fact, when I first studied LINQ-to-Sql, the automatically generated code using the boiler plate of the dbml files was actually too large to be digested at the beginning. Therefore, I began to design my POCO classes myself one at a time, and then began to study LINQ query records. Here is a quick example to get started:
I created the database "businessLinqToSql" with one table named Customer. Use the below mentioned SQL script:
USE [businessLinqToSql]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Customer](
[ID] [int] NOT NULL,
[Name] [nchar](30) NOT NULL,
[Address] [nchar](30) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Create a C # console application and add a code file named Customer.cs as shown below. This will be your POCO class with some declarative attributes, which gives a hint regarding the database schema to the LINQ-to-SQL provider:
[Table(Name="Customer")]
public class Customer
{
[Column(IsPrimaryKey = true)]
public int ID { get; set; }
[Column]
public string Name { get; set; }
[Column]
public string Address { get; set; }
}
Then you can write below LINQ query in your main function:
private static void LinqToSql()
{
DataContext dataContext = new DataContext("data source=.;initial catalog=businessLinqToSql;integrated security=True;MultipleActiveResultSets=True");
Table<Customer> customers = dataContext.GetTable<Customer>();
IQueryable<string> query = from c in customers
where c.Name.Length > 5
orderby c.Name.Length
select c.Name.ToUpper();
foreach (string name in query) Console.WriteLine(name);
}
source
share