linq-to-sql

LINQ Between Operator

The following works fine with IEnumerable types, but is there any way to get something like this working with IQueryable types against a sql database? class Program { static void Main(string[] args) { var items = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }; foreach (var item in items.Where(i => i.Between(2, 6))) ...

model an abstract base class and subclasses in a database

I have 4 subclasses: Video, Image, Note, and Form. Each one contains different types of data. For example, the Image class contains a path to the image file on disk and image properties, and the Form class contains the form field values. The common element between each item, however, is the GPS coordinates and heading, so I have the foll...

In LINQ to SQL, How to return datetime in ISO 8601 String, IETF String or timestamp?

In LINQ to SQL, how can I convert this format: "VDate(1253451600000+0200)V" to an ISO 8601 string, IETF string or timestamp format? I'm using WCF JSON-Enabled and Ajax-Enabled web service. ...

C# Linq-to-SQL Trying to recive multiple selects

Hello! I got this SQL code in a SP: (MS SQL 2008) DECLARE @type tinyint SELECT @type = Type FROM Contents WHERE ContentID = @ContentID SELECT [ParentContentID], [Headline], [ShortDescription], [CategoryID], [Type], [State], [DatePublished], [Name] FROM Contents INNER JOIN Users ON Users.ID = Contents.PublishedBy WHERE ...

Linq to SQL - Selecting a new object and performing an update

I have a query that selects data into a custom type- UserData curData = dc.Users.Where(λ => (λ.Login == username) && λ.Active) .Select( λ => new UserData { ID = λ.ID, UserName = λ.Login, isAdmin = λ.Admin, isTrusted = λ.Trusted, EMail = λ.E_mail ... }); //Yeah I know this isn't a technically correct usage of 'λ' //It is ...

LINQ to SQL: OnValidate() and custom domain model classes

Working through the NerdDinner Tutorial, I'm trying to figure out a good way to perform validation on properties that isn't dependent on a LINQ-to-SQL generated partial class. Here's some example code of what I've done so far: public abstract class DomainEntity { public IEnumerable<ValidationError> ValidationErrors { get; private s...

Linq2SQL "or/and" operators (ANDed / ORed conditions)

Let's say we need to apply several conditions to select from a table called "Things" (unknown count and nature) if conditions are known, we can write db.Things.Where(t=>foo1 && foo2 || foo3); but if we have to build that Where condition programatically, I can imagine how can we apply ANDed conditions IQuerable DesiredThings = db.Thi...

[Linq to SQL] Multiple foreign keys to the same table

I have a reference table with all sorts of controlled value lookup data for gender, address type, contact type, etc. Many tables have multiple foreign keys to this reference table I also have many-to-many association tables that have two foreign keys to the same table. Unfortunately, when these tables are pulled into a Linq model and th...

C# LINQ-to-SQL Multiple selects

Hello! I got this, its calling a SP in my MS SQL 2008 database: [Function(Name = "dbo.Content_GetContent")] [ResultType(typeof(Content_GetContentResult))] [ResultType(typeof(Content_GetContentImagesResult))] [ResultType(typeof(Content_GetContentBoxesResult))] [ResultType(typeof(Content_GetContentSearchWordsResult))] ...

How to preserve Linq2SQL OR between conditions?

Lets say we need to select two sets from a table: "Things" var GradeA = db.Things.Where(t=> condition1); var GradeB = db.Things.Where(t=> !condition1 && condition2); var DesiredList = GradeA.union(GradeB); alternatively, we need to write a single statement to avoid union cost: var DesiredList = db.Things.Where(t=> condtion1 || (!con...

Solution for Linq2SQL expressions that has no translation

Is there a way to provide translation for expressions that have no translation ? like double.parse() ...

using case in linq

How can this be written in linq select * from transactions T JOIN TransactionSample ts ON ts.TransactionID = CASE WHEN T.ParentTransactionID is null THEN T.TransactionID ELSE T.ParentTransactionID END where T.TransactionID = 227511 ...

Conditional Joins in LINQ

I have a parent child table relationship. In the example below Foo has a FooID and a nullable ParentFooID that points to a parent record. The Bar table is always linked to the parent record. This is the SQL I use to get the result. Select * from Foo f JOIN Bar b ON b.FooID = CASE WHEN f.ParentFooID is null TH...

C#, LINQ, SQL: Compound columns

I have a LINQ entity that I need to create a special string value for. It is 7 different values separated by "-". Some of the fields used are in that table, while some of them are in different tables. I would like to add this field to the entity somehow so that I don't have to create this string myself every time I need it. I was thinki...

LINQ-to-SQL IN ()

Currently I use a block of code like this, to fetch a set of DB objects with matching IDs. List<subjects> getSubjectsById(List<long> subjectIDs){ return ctx.tagSubjects.Where(t => subjectIDs.Contains(t.id)).ToList(); } But this is really inefficient, because it requires the entire table to be read from the database and then filtere...

ADO.NET + LINQ Connection = Can they reuse the same?

I have an ADO.NET connection object to an SQL Server (which will use connection pooling and all the nice things about it) in one Winforms application. We're adding some new stuff to this application and we thought that we could do it in LINQ to SQL, the question is, if we open a new connection like this: MyDataContext dc = new MyDataCo...

LinqToSql update problem when switching parents

I am trying to perform a straighforward update using LinqToSQL, and just cannot get it to work. Here's the data model: there is a timesheet_entry and customer. One customer has many timesheet_entries. It's a simple foreign key relationship. If I'm editing an existing timesheet_entry, I get an exception if I change the customer_id. Her...

What permissions should SQL Server User be granted for LinqToSql?

I am using LinqToSQL and a trusted connection to handle database selects/updates/inserts/deletes. In the past I have always used stored procedures and only granted execute permission to the Application Pool identity for the particular sproc in the database. This is my first LinqToSql project (and I really love how much it helps). I wo...

linq2sql: using ExceuteQuery<dto> when rows returned are not in my dto? Can i use a generic data type?

hi there, been using ExecuteQuery with some success, i.e. where AccessRights is my dto and queryString contains "Exec sp_name param1,param2 etc" var accessRights = this.db.ExecuteQuery<AccessRights>(queryString, sqlParams.Values.ToArray()).AsQueryable(); Everything works perfect if what returns from the stored procedure can be...

How to return dynamic return types in methods? C#

I am having a problem with the return type of a method. The method returns a linq object which at present returns type tblAppointment. This method is shown below: public tblAppointment GetAppointment(int id) { var singleAppointment = (from a in dc.tblAppointments where a.appID == ...