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

How do I tell if a linq to sql object is new, modified or unchanged?

I have a single linq to sql class. I would like to detect if it is new (meaning an insert will be done) or if there are any pending changes (update will be done). I realize that I can do this by using a partial class and hooking into the on change…
Jason Webb
  • 7,938
  • 9
  • 40
  • 49
5
votes
1 answer

LINQ-SQL Updating Multiple Rows in a single transaction

I need help re-factoring this legacy LINQ-SQL code which is generating around 100 update statements. I'll keep playing around with the best solution, but would appreciate some ideas/past experience with this issue. Here's my code: List
RPM1984
  • 72,246
  • 58
  • 225
  • 350
5
votes
1 answer

Linq 2 SQL: composite primary key

I have a linking table with two foreign keys. Together, they are the primary key for the table. I am trying to map this in Linq: [Table(Name = "PartToPart")] public class PartToPart { [Column(Name = "PartID", IsPrimaryKey = true )] public…
Christof
  • 3,777
  • 4
  • 37
  • 49
5
votes
2 answers

How dynamic can I make my LINQ To SQL Statements?

I have the need to construct a LINQ To SQL statement at runtime based on input from a user and I can't seem to figure out how to dynamically build the WHERE clause. I have no problem with the following: string Filters =…
mcass20
  • 1,668
  • 1
  • 22
  • 39
5
votes
4 answers

LINQ-to-SQL class doesn't implement INotifyPropertyChanging & INotifyPropertyChanged if pulling from local database

I modified my data source in my LINQ-to-SQL class (by the old delete and drag back in method), and was surprised to see the INotifyPropertyChanging & INotifyPropertyChanged interfaces no longer implemented in the generated classes…
Feckmore
  • 4,322
  • 6
  • 43
  • 51
5
votes
1 answer

Why are my bound parameters all identical (using Linq)?

When I run this snippet of code: string[] words = new string[] { "foo", "bar" }; var results = from row in Assets select row; foreach (string word in words) { results = results.Where(row => row.Name.Contains(word)); } I get this SQL: -- Region…
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
5
votes
4 answers

Linq to SQL - what's better?

db.Albums.FirstOrDefault(x => x.OrderId == orderId) or db.Albums.FirstOrDefault(x => x.OrderId.Equals(orderId))
Sasha
  • 20,424
  • 9
  • 40
  • 57
5
votes
3 answers

LINQ query performance issue when fetching data from db in MVC Razor

Problem Statement: I'm trying to bind Multi-table data from db to view using Linq query which is taking more time.I'm having around 10000 records in db.Someone suggested to use the IQueryable instead of IEnumerable,but does it affect my current…
Vishal I P
  • 2,005
  • 4
  • 24
  • 41
5
votes
2 answers

Sum of multiple columns in a LINQ query

I've this table: Statistics(id int, col1 int, col2 int, col3 int ...) I want to get the sum of the col1 values, col2 values, col3 values etc. A simple sql query: SELECT SUM([Col1]), SUM([Col2]) FROM [dbo].[Statistics] but in LINQ: var val1 =…
DevT
  • 1,411
  • 3
  • 16
  • 32
5
votes
3 answers

Retrieving top 50 rows from Table using LINQ

Am new to LINQ, and am trying to retrieve the top 50 rows of a particular table. In SQL Server using an actual query i coudl say "Select TOP 50 from Transactions" , but not sure how i need to do that with LinQ Any pointers that could help ? Thanks !
James
  • 53
  • 1
  • 3
5
votes
4 answers

nested linq queries, how to get distinct values?

table data of 2 columns "category" and "subcategory" i want to get a collection of "category", [subcategories] using code below i get duplicates. Puting .Distinct() after outer "from" does not help much. What do i miss? var rootcategories = (from p…
Alexander Taran
  • 6,655
  • 2
  • 39
  • 60
5
votes
4 answers

ASP.NET MVC/LINQ: What's the proper way to iterate through a Linq.EntitySet in a View?

OK so I have a strongly-typed Customer "Details" view that takes a Customer object Model. I am using LINQ to SQL and every Customer can have multiple (parking) Spaces. This is a FK relationship in the database so my LINQ-generated Customer model has…
Jeff Camera
  • 5,324
  • 5
  • 44
  • 59
5
votes
3 answers

Why are my connections not closed even if I explicitly dispose of the DataContext?

I encapsulate my linq to sql calls in a repository class which is instantiated in the constructor of my overloaded controller. The constructor of my repository class creates the data context so that for the life of the page load, only one data…
Chris Simpson
  • 7,821
  • 10
  • 48
  • 68
5
votes
2 answers

How can I bind an Enum to a DbType of bit or int?

I am using Linq2Sql and want to bind an objects field (which is enum) to either a bit or a int type in the database. For example I want have a gender field in my model. I have already edited the DBML and changed the Type to point to my enum. I want…
uriDium
  • 13,110
  • 20
  • 78
  • 138
5
votes
1 answer

Inheriting LINQ-to-SQL data context from base controller

My base controller class, BaseController, is inherited by public-facing controllers to access a shared data context between requests with LINQ-to-SQL. Am I accessing my data context in an efficient and safe way by storing it in…
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287