linq-to-sql

MVC LINQ to SQL Table Join Record Display

Hey all, Im having problems displaying records to my view when passing viewdata to a user control. This is only apparent for linq to sql objects where I am using table joins. The exception I receive is "Unable to cast object of type '<>f__AnonymousType410[System.String,System.Int32,System.Nullable1[System.DateTime],System.String,Syste...

Using Linq to Sql, how can I get a particular result from a query?

I want to get the last result returned from this query. How do I do this? Last and LastOrDefault aren't supported by Linq to Sql. var docs = (from d in db.Documents where d.Version > 1 orderby d.DocumentID select new ...

Is it possible to use/access scalar functions with LINQ to SQL?

We have scalar functions in our database for returning things like "number of tasks for a customer" or "total invoice amount for a customer". We are experimenting and looking to try to do this w/o stored procedures ... normally we would just call this function in our stored procedure and return it as a single value. Is there a way t...

LINQ2SQL 2 Data Table

We are currently mixing data sources, one is definitely SQL, whilst the other could be SQL or (currently) Oracle or DB2. We are using LINQ2SQL in our SQL only data source, however we need to use System.Data.Common objects to extract Data Tables from the other sources. We are working with Data Tables in objects using data from both thes...

Can LINQ to SQL query an XML field DB-serverside?

.NET 3.5, C# I have a web app with a "search" feature. Some of the fields that are searchable are first-class columns in the table, but some of them are in fact nested fields inside an XML data type. Previously, I built a system for dynamically constructing the SQL for my search. I had a nice class hierarchy that built SQL expressio...

C#: ?: Operator in LINQ Query

How do I utilize a ?: operator in the SELECT clause of a LINQ query? If this can't be done, how can I emulate one? The goal is to get a CASE block in my select clause. As you might suspect, I'm getting an error: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or m...

Linq to SQL Foreign Keys

DDL for Database Tables: Users: id - int - identity name - varchar - unique PCs: id - int - idnetity name - varchar - unique userid - FK to Users Apps: id - int - identity name - varchar pcid - FK to PCs I created a DataContext using the Linq To SQL designer in Visual Studio 2008. I want ...

Visual C# 2008 EE SP1 - Linq to SQL - Data Connection to remote server?

After upgrading my Visual C# 2008 Express Edition to .NET3.5, SP1, I've been unable to create new LINQ to SQL classes using a remotely connected database. I used to be able to do so fine. I open up a project (windows forms, class library, same behavior), and either use the New -> Linq To SQL classes method or go directly for adding the ...

Linq to SQL, Stored Procedure with different return types based on an If/Else

I have an existing Stored Procedure which I am trying to now call with LINQ to SQL, here is the stored procedure: ALTER procedure [dbo].[sp_SELECT_Security_ALL] ( @UID Varchar(15) ) as DECLARE @A_ID int If ISNULL(@UID,'') = '' SELECT DISTINCT App_ID, App_Name, App_Description, DB, DBNameApp...

LINQ-to-SQL: Stored Procedure that returns a single scalar value ?

I am using LINQ-to-SQL for an application that queries a legacy database. I need to call a stored procedure, that selects a single integer value. Changing the stored procedure is not an option. The designer creates a method with this signature: private ISingleResult<sp_xal_seqnoResult> NextRowNumber([Parameter(DbType="Int")] System.Nul...

Insert into multiple database tables using Linq, ASP.NET MVC.

I have a rather simple scenario where I have two tables in which I want to add data. They are managed with primary key/foreign key. I want to add new data into TABLE A and then retrieve the Id and insert into TABLE B. I can certainly do it with a stored procedure, but I'm looking at trying to do it using Linq. What is the best approa...

Submit changes of only one entity

If I select many rows from one table with one instance of DataContext. And then do I some changes in properties in the rows, can I submit changes to database only for one of the selected rows? ...

Streaming and Linq Blobs

I have an object I am using to store document meta data into a table. The body text of the document can be very large, sometimes > 2GB so I will be storing it into a nvarchar(max) field in SQL 2008. I'll use SQL 2008 later to index that field. I won't be using filestreams because they are very restrictive to the database and prevents ...

How do I do a dynamic Linq2SQL Query?

I'm trying to build a method that will receive a Linq table, and should return a List<> of values that will be a DropDownList Datasource. This is what I've got till now: public static List<Structs.NameValuePair> GenDropDownItens<T>(string ValueField , string TextField ) where T: class What i don't know how to do is, query the table g...

ASP.NET MVC Caching vary by authentication

Hello, I'm using ASP.NET Preview 5 (will upgrade soon to Beta) with LINQ2SQL on my recently launched tiny webapp. I was so proud of my work until Silverlight.net featured it on the first page and it started receiving a more than humble number of visitors. For some reason, users are sometimes getting "Specified cast invalid" thrown by ...

Parallel LINQ in WebApps?

I just watched the last Channel 9 vid on the upcoming parallel extensions to .NET. How would you use this in a web app? I'm specifically thinking of using the parallel Linq extensions against a SQL db. Would this makes sense to use as a way to speed up your data access layer in a multi-user server app? What are the issues (aside from the...

Date Comparison in Dynamic Where Clause in Linq Gives Overload Exception

I'm trying to inject a dynamic where clause in my Linq to SQL query and I get an overload exception. The same expression work when added in the query proper? qry.Where(Function(c) c.CallDate < Date.Now.AddDays(-1)) Any thoughts on how to this to work? The exception reads: Overload resolution failed because no accessible 'Where' ca...

Linq-to-SQL How to prevent the use of Delete methods?

By convention our DB only alows the use of stored procedures for INSERT, UPDATE and DELETE. For some tables / types there is no DELETE stored procedure, because it is not allowed to delete rows. (You can only update the status of such a type to "deleted"). e.g. a customer may be marked as deleted but is never really removed from the DB. ...

LINQ to SQL bug (or very strange feature) when using IQueryable, foreach, and multiple Where

I ran into a scenario where LINQ to SQL acts very strangely. I would like to know if I'm doing something wrong. But I think there is a real possibility that it's a bug. The code pasted below isn't my real code. It is a simplified version I created for this post, using the Northwind database. A little background: I have a method that take...

Is LINQ to SQL the best way to build a Model or create my own classes

I am develop a medium system in ASP.net with MS SQL Server Database and I wonder what is the best way to create a model layer with LINQ or create own classes that dealing with database? ...