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
1337
votes
20 answers

Difference Between Select and SelectMany

I've been searching the difference between Select and SelectMany but I haven't been able to find a suitable answer. I need to learn the difference when using LINQ To SQL but all I've found are standard array examples. Can someone provide a LINQ To…
Tarik
  • 79,711
  • 83
  • 236
  • 349
1211
votes
14 answers

Returning IEnumerable vs. IQueryable

What is the difference between returning IQueryable vs. IEnumerable, when should one be preferred over the other? IQueryable custs = from c in db.Customers where c.City == "" select c; IEnumerable custs = from c in…
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
867
votes
17 answers

Entity Framework vs LINQ to SQL

Now that .NET v3.5 SP1 has been released (along with VS2008 SP1), we now have access to the .NET entity framework. My question is this. When trying to decide between using the Entity Framework and LINQ to SQL as an ORM, what's the difference? The…
Chris Roberts
  • 18,622
  • 12
  • 60
  • 67
613
votes
17 answers

LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria

Consider the IEnumerable extension methods SingleOrDefault() and FirstOrDefault() MSDN documents that SingleOrDefault: Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is…
p.campbell
  • 98,673
  • 67
  • 256
  • 322
480
votes
18 answers

What is the syntax for an inner join in LINQ to SQL?

I'm writing a LINQ to SQL statement, and I'm after the standard syntax for a normal inner join with an ON clause in C#. How do you represent the following in LINQ to SQL: select DealerContact.* from Dealer inner join DealerContact on…
Glenn Slaven
  • 33,720
  • 26
  • 113
  • 165
445
votes
5 answers

Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures?

How would you rate each of them in terms of: Performance Speed of development Neat, intuitive, maintainable code Flexibility Overall I like my SQL and so have always been a die-hard fan of ADO.NET and stored procedures but I recently had a play…
BritishDeveloper
  • 13,219
  • 9
  • 52
  • 62
424
votes
4 answers

Convert Linq Query Result to Dictionary

I want to add some rows to a database using Linq to SQL, but I want to make a "custom check" before adding the rows to know if I must add, replace or ignore the incomming rows. I'd like to keep the trafic between the client and the DB server as low…
Tipx
  • 7,367
  • 4
  • 37
  • 59
222
votes
9 answers

How do I update a Linq to SQL dbml file?

How do I update a Linq to SQL .dbml file?
Ante
  • 8,567
  • 17
  • 58
  • 70
212
votes
3 answers

How to do a join in linq to sql with method syntax?

I have seen lots of examples in LINQ to SQL examples on how to do a join in query syntax but I am wondering how to do it with method syntax? For example how might I do the following var result = from sc in enumerableOfSomeClass join soc…
chobo2
  • 83,322
  • 195
  • 530
  • 832
207
votes
16 answers

Return anonymous type results?

Using the simple example below, what is the best way to return results from multiple tables using Linq to SQL? Say I have two tables: Dogs: Name, Age, BreedId Breeds: BreedId, BreedName I want to return all dogs with their BreedName. I should…
Jonathan S.
  • 5,837
  • 8
  • 44
  • 63
202
votes
17 answers

Max or Default?

What is the best way to get the Max value from a LINQ query that may return no rows? If I just do Dim x = (From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter).Max I get an error when the query returns no…
gfrizzle
  • 12,419
  • 19
  • 78
  • 104
200
votes
13 answers

What's the Linq to SQL equivalent to TOP or LIMIT/OFFSET?

How do I do this Select top 10 Foo from MyTable in Linq to SQL?
Herb Caudill
  • 50,043
  • 39
  • 124
  • 173
191
votes
22 answers

LINQ-to-SQL vs stored procedures?

I took a look at the "Beginner's Guide to LINQ" post here on StackOverflow (Beginners Guide to LINQ), but had a follow-up question: We're about to ramp up a new project where nearly all of our database op's will be fairly simple data retrievals…
Scott Marlowe
  • 7,915
  • 10
  • 45
  • 51
190
votes
9 answers

Learning about LINQ

Overview One of the things I've asked a lot about on this site is LINQ. The questions I've asked have been wide and varied and often don't have much context behind them. So in an attempt to consolidate the knowledge I've acquired on Linq I'm posting…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
189
votes
3 answers

Can I return the 'id' field after a LINQ insert?

When I enter an object into the DB with Linq-to-SQL can I get the id that I just inserted without making another db call? I am assuming this is pretty easy, I just don't know how.
naspinski
  • 34,020
  • 36
  • 111
  • 167
1
2 3
99 100