linq-to-sql

How to rollback the datacontext to the point of the last submitchanges

I am working on an app that is inserting a lot of new objects (rows) and relationships between them. But at a certain point when an error occurs I want all the changes to the DataContext be disgarded and "thrown away". So that after an error I have a clean copy of the DataContext that matches the state of the database. ...

How to use transactions with a datacontext

Can i use transactions with a datacontext, so i can rollback the state of the context after an error. And so yes, how does it work? ...

Using LINQ to SQL to copy between databases.

Given two databases of identical schema, is there any easy way to transfer records between the two? I'm using LINQ to SQL as I need to perform a few additional operations along the way and having objects representing the records I'm transferring makes this much easier. This is a small-scale helper app, so SSIS is probably overkill and SQ...

SQL view data not updating properly

I have two tables (Knicks and Knacks) and a view that joins the two tables (PaddyWhacks). The tables have the following fields: Knicks.key, Knicks.data1 Knacks.fkey, Knacks.data2 So the view contains: PaddyWhacks.key, PaddyWhacks.data1, PaddyWhacks.data2 I'm using LINQ to SQL to retrieve the full view as IQueryable and everything...

"Bad Storage property" error on LINQ to Stored Procedure

I just added a newly created SP to my project which unfortunately required a temp table to operate, so I had to build my RESULT class for the SP manually. Now when I try to run I get a "Bad Storage Property" error on the below property. public partial class sp_One_EVA_Get_User_InformationResult { private string _Security_USER_ID; ...

Linq to SQL error SQL does not compare NText, Text, Xml, or Image?

When trying to do an update for a Linq object I receive the following error. "SQL Server does not handle comparison of NText, Text, Xml, or Image data types." There is a field in the database that is a Text field because it is going to hold a string that could be as much as 32kb. Should I change the data type or is there a work around...

Best way to delete multiple records in a LINQ query?

What is the best way to multiple records in one go in LINQ? ...

Best practise for the LINQ to SQL entity persistance between ASP.NET requests

For my current project I'm using LINQ to SQL as ORM. For now I'm storing modified entities in session to save changes made between ASP.NET requests. What is the best practise for doing this, because lately I started seing this approach is not very scalable for the Web farms. ...

Custom RoleProvider: Can't insert record in UsersInRole Table

Hi, I have implemented a LINQ to SQL based RoleProvider, when I assign role to a user following exception is thrown while AddUsersToRoles method is called. I have defined a composite primary key userid & roleId on this table, it still throwing this exception: Can't perform Create, Update or Delete operations on 'Table(UsersInRole)'...

How to select only Date value No Time in LINQ to SQL?

I need to have a query which list all the user according to the date value in the database. Problems is this, in the database the date format is 5/5/2009 4:30:12 but I want to compare with 5/5/2009. I think the value of the based date is 5/5/2009 12:00:00 and that's why I couldn't query it. The query is something like dim db = new dat...

How do you abstract out your persistence code when using LINQ to SQL?

I love LINQ to SQL but it has been bugging me that in using it, my repository code becomes generated by the LINQ to SQL framework and hence tightly coupled to an SQL Server database. Are any of you using LINQ to SQL in an abstracted, loosely coupled fashion and if so, how did you approach the problem of keeping your code database-indepe...

Linq2Sql Insert Records To Related Tables

Hi There, Similar situation to : http://stackoverflow.com/questions/630320/how-to-add-several-dependent-records-with-linq2sql (this seems logical, but it doesn't work for me) ASP.NET MVC + Linq2SQL I have 2 tables called Challenges and Participants. Challenge { challengeId, ChallengeDesc, applicantId, respondantId } Participants {...

How To Define The Return Type In A Function With LINQ?

I would like to know how to define a returntype in a function in following situation. I've got a products and I was returning all information or one product at a time. as you can see in my function defined below. public static Products GetProducts(int pid) { var pro = from p in context.Products select p; if(pid...

How to display Duration only Hours:Minutes:Second in Gridview Asp.Net by using LINQ to SQL?

Hi again Guys, I want to display the duration only Hour, Minutes, and Second in data Gridview by Subtract TimeCheckOut from TimeCheckIn in ASP.NET using LINQ to SQL Here is code behind: Dim db = new MyDataContext Dim user = from u in db.Employees select IDNumber = u.IDNumber, _ FirstName = u.firstName, LastName = u.lastName,...

Linqtosql - Find all entities matching all tags in a query

I have the classic 3 table - entity, tag and entitytag - database structure. In order to find all entities tagged with certain tags I am using the following Linqtosql code: string[] myTags = {"tag1","tag2"}; var query = from m in entity where m.entitytag.Where(c => myTags.Contains(c.tag.TagName)).Count() == myTags.Count() ...

Enum with default typecast? is that possible?

is it possible to make a default typecast for an enum? I use enum for a lot, such as states and I want to compare enums directly to LINQ fields, but I have to typecast all the time. ...

GroupJoin vs. Where to filter out null related items

Is there any advantage in using either of these to retrieve elements from TableA that don't have a related element in TableB? TableA .GroupJoin( TableB, o => o.TableAID, i => i.TableAID, (o,i) => new {o, child = i.DefaultIfEmpty()}) .Where(x => x.child.Where(c => c != null).Count() == 0) .Select(x => x.o...

Making Linq To SQL DRY.

We decided to use Linq To SQL for our Data Layer on our most recent project. We have a functional solution, that so far has handled everything we have thrown at it, with one major problem. We have to code the same method over and over again to retrieve just slightly different result sets from our database. As an example: pu...

How to write a ContainsAll query using LINQ? (C# LINQ-TO-SQL)

Suppose i have the following var mustHave = new int [] { 1, 2 } and a table named db.Numbers public class Number { public int id; public int number; } populated with Numbers.add(new Number(){id=8,number=1}); Numbers.add(new Number(){id=8,number=2}); Numbers.add(new Number(){id=8,number=3}); Numbers.add(new Number(){id=9,...

C# Linq-SQL: An UpdateByID method for the Repository Pattern

I have implemented a sort of Repository class and it has has GetByID, DeleteByID methods and so on, but I'm having trouble implementing the UpdateByID method. I did something like this: public virtual void UpdateByID(int id, T entity) { var dbcontext = DB; var item = GetByID(dbcontext, id); item = entity; ...