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
1 answer

Duplicate a database record with linq

Is there a way to duplicate a db record with linq to sql in c#? Id [int] IDENTITY(1,1) NOT NULL PRIMARY KEY, [Foo] [nvarchar](255) NOT NULL, [Bar] [numeric](28,12) NOT NULL, ... Given the table above, I would like to duplicate a record (but give it…
holz
  • 976
  • 1
  • 11
  • 21
5
votes
8 answers

Linq - how does it work?

I have just been looking into Linq with ASP.Net. It is very neat indeed. I was just wondering - how do all the classes get populated? I mean in ASP.Net, suppose you have a Linq file called Catalogue, and you then use a For loop to loop through…
ClarkeyBoy
  • 4,934
  • 12
  • 49
  • 64
5
votes
1 answer

LINQ to SQL translation to SQL of custom method

Is there a way to translate an expression to SQL to use with LINQ to SQL? For example I have a method that compares two values. Example: MyComparer.Compare(value1, value2, ">") return value1 > value2 MyComparer.Compare(value1, value2, "=") return…
DJPB
  • 5,429
  • 8
  • 30
  • 44
5
votes
4 answers

LINQ To SQL ignore unique constraint exception and continue

I have a single table in a database called Users Users ------ ID (PK, Identity) Username (Unique Index) I have setup a unique index on the Username table to prevent duplicates. I am then enumerating through a collection and creating a new user in…
Martin
  • 51
  • 1
  • 2
5
votes
6 answers

Simple way to return anonymous types (to make MVC using LINQ possible)

I'd like to implement MVC while using LINQ (specifically, LINQ-to-entities). The way I would do this is have the Controller generate (or call something which generates) the result-set using LINQ, then return that to the View to display the data. …
BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
5
votes
3 answers

Linq To SQL - Specified cast is not valid - SingleOrDefault()

I am trying to do the following... Request request = ( from r in db.Requests where r.Status == "Processing" && r.Locked == false select r ).SingleOrDefault(); It is throwing the following exception... Message: Specified cast is not…
NullReference
  • 83
  • 1
  • 2
  • 4
5
votes
1 answer

Error casting FieldExpression to LambdaExpression using Linq to SQL

I'm getting the error Unable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.LambdaExpression' when running the below code. The intent of this code is to allow me to filter records (Entity Framework…
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
5
votes
0 answers

TransactionScope works in some places and not in others

Using ASP.NET 3.5, Linq to SQL, SQL Server 2005 on Windows Server 2003. Running VS 2008 on XP SP3 locally. We need to be able to wrap inserts, updates, and deletes in a transaction. When we first tried this by wrapping code blocks with using(var…
Byron Sommardahl
  • 12,743
  • 15
  • 74
  • 131
5
votes
2 answers

Parent-Child one-to-one Relation same Table

I have a question about how to realize a Table relationship in Microsoft SQL-Server 2012. I have a Table (MyTable) which should hold a Parent/Child structure. One Parent may have one Child and one Child only has one Parent. This is a classical…
sternze
  • 1,524
  • 2
  • 9
  • 15
5
votes
4 answers

Linq to SQL gives NotSupportedException when using local variables

It appears to me that it matters whether you use a variable to temporary store an IQueryable or not. See the simplified example below: This works: List jobNames = new List { "ICT" }; var ictPeops = from p in dataContext.Persons …
nicojs
  • 1,879
  • 2
  • 18
  • 34
5
votes
2 answers

Debugging a tough LINQ-to-SQL Timeout

I'm getting a timeout error when trying to execute a LINQ (-to-SQL) query System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Now, this is not just…
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
5
votes
1 answer

What's faster? Struct array or DataTable

I am using LinqToSQL to process data from SQL Server to dump it into an iSeries server for further processing. More details on that here. My problem is that it is taking about 1.25 minutes to process those 350 rows of data. I am still trying to…
Mike Wills
  • 20,959
  • 28
  • 93
  • 149
5
votes
1 answer

Linq to Sql DB Object to Domain Object mapping and performance

I'm having a problem trying to make my LINQ to SQL queries and the mapping to my domain objects DRY without incurring the cost of multiple round trips to the db. Given this example: var query1 = from x in db.DBProducts select new…
tbehunin
  • 1,043
  • 1
  • 12
  • 24
5
votes
2 answers

Linq search that ignores nulls

How can I make a linq search that ignores nulls (or nullables)? I have a method IEnumerable Search(int? a, int? b, int? c) And I want it to return matches on any of the ints? that are not null. IE: if a and c have values 1 and 9 and b is null…
C. Ross
  • 31,137
  • 42
  • 147
  • 238
5
votes
14 answers

LINQ to SQL -

I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: Message: Specified cast is not valid. Type:…
Paul Prewett