Questions tagged [linq-to-sql]

This tag is NOT ABOUT LINQ TO SQL TRANSLATION! Linq to SQL is an old ORM that's part of .Net framework, not core. For questions about LINQ to SQL translation please use the tag of the ORM in question.

LINQ to SQL

LINQ to SQL is a component of .NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects stored in Microsoft SQL Server. The tag linq-to-entities should be used for questions about Entity Framework LINQ queries.

LINQ to SQL maps the data model of a relational database to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates the language-integrated queries in the object model into SQL and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language.

As of 2008, LINQ to SQL is no longer being actively developed, and Microsoft recommends using Entity Framework instead.

Introduction

Using LINQ to SQL

More information

14803 questions
82
votes
3 answers

Multiple WHERE clause in Linq

I'm new to LINQ and want to know how to execute multiple where clause. This is what I want to achieve: return records by filtering out certain user names. I tried the code below but not working as expected. DataTable tempData =…
Ganesha
  • 1,531
  • 3
  • 16
  • 22
80
votes
6 answers

How to do a subquery in LINQ?

Here's an example of the query I'm trying to convert to LINQ: SELECT * FROM Users WHERE Users.lastname LIKE '%fra%' AND Users.Id IN ( SELECT UserId FROM CompanyRolesToUsers WHERE CompanyRoleId in (2,3,4) ) There is…
marcel_g
  • 1,980
  • 2
  • 17
  • 19
79
votes
9 answers

Get sum of two columns in one LINQ query

let's say that I have a table called Items (ID int, Done int, Total int) I can do it by two queries: int total = m.Items.Sum(p=>p.Total) int done = m.Items.Sum(p=>p.Done) But I'd like to do it in one query, something like this: var x = from p in…
Axarydax
  • 16,353
  • 21
  • 92
  • 151
79
votes
5 answers

NOLOCK with Linq to SQL

Is it possible to get Linq2Sql to emit a NOLOCK in its SQL? And if so, how?
Scott McKenzie
  • 16,052
  • 8
  • 45
  • 70
77
votes
5 answers

TransactionScope vs Transaction in LINQ to SQL

What are the differences between the classic transaction pattern in LINQ to SQL like: using(var context = Domain.Instance.GetContext()) { try { context.Connection.Open(); context.Transaction =…
Ben McNiel
  • 8,661
  • 10
  • 36
  • 38
76
votes
7 answers

Best way to update LINQ to SQL classes after database schema change

I'm using LINQ to SQL classes in a project where the database design is still in a bit of flux. Is there an easy way of synchronising the classes with the schema, or do I need to manually update the classes if a table design changes?
Compile This
  • 11,892
  • 2
  • 25
  • 22
74
votes
3 answers

To return IQueryable or not return IQueryable

I have a repository class that wraps my LINQ to SQL Data Context. The repository class is a business line class that contains all the data tier logic (and caching and such). Here's my v1 of my repo interface. public interface ILocationRepository { …
CVertex
  • 17,997
  • 28
  • 94
  • 124
74
votes
14 answers

Linq query with nullable sum

from i in Db.Items select new VotedItem { ItemId = i.ItemId, Points = (from v in Db.Votes where b.ItemId == v.ItemId select v.Points).Sum() } I got this query, however it fails if no votes are found with…
AndreasN
  • 2,881
  • 3
  • 24
  • 29
72
votes
3 answers

LINQ to SQL Where Clause Optional Criteria

I am working with a LINQ to SQL query and have run into an issue where I have 4 optional fields to filter the data result on. By optional, I mean has the choice to enter a value or not. Specifically, a few text boxes that could have a value or…
RSolberg
  • 26,821
  • 23
  • 116
  • 160
71
votes
3 answers

LINQ: combining join and group by

I have a query that combines a join and a group, but I have a problem. The query is like: var result = from p in Products join bp in BaseProducts on p.BaseProductId equals bp.Id group p by p.SomeId…
L-Four
  • 13,345
  • 9
  • 65
  • 109
70
votes
1 answer

How to use union all in LINQ?

How to use union all in LINQ TO SQL. I have use the following code for union, then how to use this for union all? List lstTbEmployee = obj.tbEmployees.ToList(); List lstTbEmployee2 = (from a in lstTbEmployee …
Brillian
  • 1,411
  • 2
  • 16
  • 23
69
votes
3 answers

How to create a LINQ to SQL Transaction?

I have a piece of code that involves multiple inserts but need to execute submitchanges method before I finish inserting in other tables so that I can aquire an Id. I have been searching through the internet and couldnt find how to create a…
Drahcir
  • 12,311
  • 19
  • 63
  • 76
69
votes
5 answers

Purpose of EF 6.x DbContext Generator option when adding a new data item in Visual Studio

I have a web app that I built using LINQ to SQL and I'm looking to upgrade it to LINQ to Entity Framework. I've looked at some tutorials and what I've learned is that basically in the database-first scenario, you create an ADO.NET Entity Data Model.…
frenchie
  • 51,731
  • 109
  • 304
  • 510
68
votes
1 answer

Find (search for) table in DBML designer quickly?

We have a design with 150+ tables and in the .dbml layout has lines everywhere and it's hard to find individual tables. Is there a way to search in the dbml layout instead of scrolling around trying to locate a table (Ctrl+F don't work), Running…
Andreas
  • 6,958
  • 10
  • 38
  • 52
68
votes
24 answers

Doesn't Linq to SQL miss the point? Aren't ORM-mappers (SubSonic, etc.) sub-optimal solutions?

I'd like the community's take on some thoughts I've had about Linq to Sql and other ORM mappers. I like Linq to Sql and the idea of expressing data access logic (or CRUD operations in general) in your native development tongue rather than having to…
Mark Brittingham
  • 28,545
  • 12
  • 80
  • 110