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
176
votes
5 answers

LINQ - Left Join, Group By, and Count

Let's say I have this SQL: SELECT p.ParentId, COUNT(c.ChildId) FROM ParentTable p LEFT OUTER JOIN ChildTable c ON p.ParentId = c.ChildParentId GROUP BY p.ParentId How can I translate this into LINQ to SQL? I got stuck at the COUNT(c.ChildId), the…
pbz
  • 8,865
  • 14
  • 56
  • 70
173
votes
15 answers

How to store a list in a column of a database table

So, per Mehrdad's answer to a related question, I get it that a "proper" database table column doesn't store a list. Rather, you should create another table that effectively holds the elements of said list and then link to it directly or through a…
JnBrymn
  • 24,245
  • 28
  • 105
  • 147
161
votes
6 answers

Linq to Sql: Multiple left outer joins

I'm having some trouble figuring out how to use more than one left outer join using LINQ to SQL. I understand how to use one left outer join. I'm using VB.NET. Below is my SQL syntax. T-SQL SELECT o.OrderNumber, v.VendorName, …
Bryan Roth
  • 10,479
  • 15
  • 47
  • 56
158
votes
7 answers

LINQ to SQL - Left Outer Join with multiple join conditions

I have the following SQL, which I am trying to translate to LINQ: SELECT f.value FROM period as p LEFT OUTER JOIN facts AS f ON p.id = f.periodid AND f.otherid = 17 WHERE p.companyid = 100 I have seen the typical implementation of the left outer…
dan
  • 5,664
  • 8
  • 45
  • 59
152
votes
8 answers

LINQ to SQL: Multiple joins ON multiple Columns. Is this possible?

Given: A table named TABLE_1 with the following columns: ID ColumnA ColumnB ColumnC I have SQL query where TABLE_1 joins on itself twice based off of ColumnA, ColumnB, ColumnC. The query might look something like this: Select t1.ID, t2.ID, t3.ID …
aarona
  • 35,986
  • 41
  • 138
  • 186
148
votes
6 answers

LINQ to SQL Left Outer Join

Is this query equivalent to a LEFT OUTER join? //assuming that I have a parameter named 'invoiceId' of type int from c in SupportCases let invoice = c.Invoices.FirstOrDefault(i=> i.Id == invoiceId) where (invoiceId == 0 || invoice != null) …
Ali Kazmi
  • 3,610
  • 6
  • 35
  • 51
140
votes
9 answers

Linq: adding conditions to the where clause conditionally

I have a query like this (from u in DataContext.Users where u.Division == strUserDiv && u.Age > 18 && u.Height > strHeightinFeet select new DTO_UserMaster { Prop1 = u.Name, }).ToList(); I want…
user20358
  • 14,182
  • 36
  • 114
  • 186
136
votes
5 answers

How do you perform a CROSS JOIN with LINQ to SQL?

How do you perform a CROSS JOIN with LINQ to SQL?
Luke Smith
  • 23,504
  • 8
  • 29
  • 28
132
votes
6 answers

How to select only the records with the highest date in LINQ

I have a table, 'lasttraces', with the following fields. Id, AccountId, Version, DownloadNo, Date The data looks like this: 28092|15240000|1.0.7.1782|2009040004731|2009-01-20 13:10:22.000 28094|61615000|1.0.7.1782|2009040007696|2009-01-20…
Bas Jansen
  • 1,710
  • 2
  • 14
  • 12
123
votes
10 answers

efficient way to implement paging

Should I use LINQ's Skip() and Take() method for paging, or implement my own paging with a SQL query? Which is most efficient? Why would I choose one over the other? I'm using SQL Server 2008, ASP.NET MVC and LINQ.
StoneHeart
  • 15,790
  • 32
  • 67
  • 84
117
votes
14 answers

Random row from Linq to Sql

What is the best (and fastest) way to retrieve a random row using Linq to SQL when I have a condition, e.g. some field must be true?
Julien Poulin
  • 12,737
  • 10
  • 51
  • 76
117
votes
9 answers

NHibernate vs LINQ to SQL

As someone who hasn't used either technology on real-world projects I wonder if anyone knows how these two complement each other and how much their functionalities overlap?
Manu
  • 28,753
  • 28
  • 75
  • 83
110
votes
15 answers

A dependent property in a ReferentialConstraint is mapped to a store-generated column

I get this error when writing to the database: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'PaymentId'. public bool PayForItem(int terminalId, double paymentAmount, …
Welsh King
  • 3,178
  • 11
  • 38
  • 60
110
votes
6 answers

Linq to SQL how to do "where [column] in (list of values)"

I have a function where I get a list of ids, and I need to return the a list matching a description that is associated with the id. E.g.: public class CodeData { string CodeId {get; set;} string Description {get; set;} } public…
Nathan
  • 10,593
  • 10
  • 63
  • 87
109
votes
4 answers

What is "Audit Logout" in SQL Server Profiler?

I'm running a data import (using C#/Linq), and naturally I'm trying to optimize my queries as much as possible. To this end I'm running a trace on the DB using SQL Server Profiler, with my trace filtered by my SQL login name (it's a name that can…
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387