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
108
votes
16 answers

Apply properties values from one object to another of the same type automatically?

Given 2 objects A and B of type T, I want to assign the properties' values in A to the same properties in B without doing an explicit assignment for each property. I want to save code like this: b.Nombre = a.Nombre; b.Descripcion =…
eKek0
  • 23,005
  • 25
  • 91
  • 119
106
votes
16 answers

What can I do to resolve a "Row not found or changed" Exception in LINQ to SQL on a SQL Server Compact Edition Database?

When executing SubmitChanges to the DataContext after updating a couple properties with a LINQ to SQL connection (against SQL Server Compact Edition) I get a "Row not found or changed." ChangeConflictException. var ctx = new…
Kevin
  • 9,309
  • 12
  • 44
  • 51
105
votes
7 answers

How do I get the MAX row with a GROUP BY in LINQ query?

I am looking for a way in LINQ to match the follow SQL Query. Select max(uid) as uid, Serial_Number from Table Group BY Serial_Number Really looking for some help on this one. The above query gets the max uid of each Serial Number because of the…
SpoiledTechie.com
  • 10,515
  • 23
  • 77
  • 100
105
votes
3 answers

how to update the multiple rows at a time using linq to sql?

Table: id userid friendid name status 1 1 2 venkat false 2 1 3 sai true 3 1 4 arun false 4 1 5 arjun false If a user sends userid=1,friendids=2,4,5…
user1237131
  • 1,853
  • 3
  • 26
  • 35
103
votes
13 answers

How can I conditionally apply a Linq operator?

We're working on a Log Viewer. The use will have the option to filter by user, severity, etc. In the Sql days I'd add to the query string, but I want to do it with Linq. How can I conditionally add where-clauses?
sgwill
  • 9,814
  • 8
  • 35
  • 40
101
votes
6 answers

Understanding SQL Server LOCKS on SELECT queries

I'm wondering what is the benefit to use SELECT WITH (NOLOCK) on a table if the only other queries affecting that table are SELECT queries. How is that handled by SQL Server? Would a SELECT query block another SELECT query? I'm using SQL Server 2012…
Francis P
  • 13,377
  • 3
  • 27
  • 51
96
votes
16 answers

Error - SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

I've been using this piece of code I've written and it's working in this most unclear manner. I wish to insert a row into the database which includes two columns of DateTime: myrow.ApprovalDate = DateTime.Now myrow.ProposedDate = DateTime.Now And…
vondiplo
96
votes
16 answers

Is LINQ to SQL Dead or Alive?

Just when I make friends with LINQ to SQL, it appears as though MS is pulling the rug out from under it. http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to-entities-roadmap.aspx From my little bit of research, EF is…
rp.
  • 17,483
  • 12
  • 63
  • 79
95
votes
5 answers

Linq to Sql: How to quickly clear a table

To delete all the rows in a table, I am currently doing the following: context.Entities.DeleteAllOnSubmit(context.Entities); context.SubmitChanges(); However, this seems to be taking ages. Is there a faster way?
Svish
  • 152,914
  • 173
  • 462
  • 620
94
votes
6 answers

Instantiate empty IQueryable for use with Linq to sql

I need to be able to either have an optional parameter in a Linq query, or be able to assign the query to a var in something like an IF if that optional parameter needs to be removed from the query. If I set the query var inside the IF statement…
user2073077
  • 977
  • 1
  • 7
  • 7
91
votes
6 answers

Missing LINQ to SQL Classes (DBML designer) in Visual Studio 2017 RC

How can I add LINQ to SQL class (.dbml file) to Visual Studio 2017 RC. I can't find it. Please help, thanks.
Çağatay Ay
  • 973
  • 1
  • 7
  • 8
89
votes
4 answers

Linq-to-SQL ToDictionary()

How do I properly convert two columns from SQL (2008) using Linq into a Dictionary (for caching)? I currently loop through the IQueryable b/c I can't get the ToDictionary method to work. Any ideas? This works: var query = from p in db.Table …
Codewerks
  • 5,884
  • 5
  • 29
  • 33
88
votes
4 answers

Linq to SQL - Return top n rows

I want to return the TOP 100 records using Linq.
jinsungy
  • 10,717
  • 24
  • 71
  • 79
88
votes
10 answers

How to do Select All(*) in linq to sql

How do you select all rows when doing linq to sql? Select * From TableA In both query syntax and method syntax please.
chobo2
  • 83,322
  • 195
  • 530
  • 832
84
votes
2 answers

ASP MVC: When is IController Dispose() called?

I'm going through a big refactoring / speed tweaking of one of my larger MVC apps. It has been deployed to production for a few months now, and I was starting to get timeouts waiting for connections in the connection pool. I have tracked the issue…
John Gietzen
  • 48,783
  • 32
  • 145
  • 190