linq-to-sql

Linq2Sql Best Practices

I'm recently migrating to Linq2Sql and all my future projects will be using Linq2Sql. Having said that, I researched a lot on how to properly plug-in Linq2Sql in application design. what to put at what layer ? How do you design your repositories and business layer services ? Should I use DTOs over Linq2Sql entities on interaction lay...

Will my LinqToSql execution be deffered if i filter with IEnumerable<T> instead of IQueryable<T>?

I have been using these common EntityObjectFilters as a "pipes and filters" way to query from a collection a particular item with an ID: public static class EntityObjectFilters { public static T WithID<T>(this IQueryable<T> qry, int ID) where T : IEntityObject { return qry.SingleOrDefault<T>(item => item.ID == ID)...

Linq2Sql - attempting to update but the Set statement in sql is empty

This is weird ... done updates loads of times before but cannot spot why this is different. although I am using .net 4.0 now - however i doubt its a bug in its L2S implementation. Its not like this is a wierd and wonderful application of it. Although i am fairly sure this code worked whilst i was using the RC. I have also managed to rep...

nested linq-to-sql queries

var result = ( from contact in db.Contacts join user in db.Users on contact.CreatedByUserID equals user.UserID orderby contact.ContactID descending select new ContactListView { ContactID = contact.ContactID, FirstName = contact.FirstName, LastName = contact.LastName...

Is opening too many datacontexts bad?

I've been checking my application with linq 2 sql profiler, and I noticed that it opens a lot of datacontexts, most of them are opened by the linq datasource I used, since my repositories use only the instance stored in Request.Items, is it bad to open too many datacontext? and how can I make my linqdatasource to use the datacontext that...

How can i mock or test my deferred evaluation/execution functionality?

I have what could be seen as a bizarre hybrid of IQueryable<T> and IList<T> collections of domain objects passed up my application stack. I'm trying to maintain as much of the 'late querying' or 'lazy loading' as possible. I do this in two ways: By using a LinqToSql data layer and passing IQueryable<T>s through by repositories and to m...

Can I specify a Stored Procedure in my LinqDataSource ?

I have a Listview that I want to read the results of a Stored procedure. I have created the DBML object with the table and the stored procedure. When I configure my LinqDataSource on the page I cannot specify the SProc - only the Table. Or do I need to do it in the Listview ? Thanks ...

[Linq to sql] query result what should i use Count() or Any()...

I am checking login of a user by this repository method, public bool getLoginStatus(string emailId, string password) { var query = from r in taxidb.Registrations where (r.EmailId == emailId && r.Password==password) select r; if (query.Count() != 0) { retur...

Linq-To-Sql ClearCache() Method - Ok to call?

I want to be able to clear the database context cache in order to clear the entities the context is currently tracking. The context object has a ClearCache method, but it's internal. I wrote the following code to call this method: private void ClearContextCache() { const BindingFlags FLAGS = BindingFlags.Instance | BindingFlags.Publ...

[Linq-To-Sql]How to return two values of different datatypes?

Here is my repository method which returns UserId , public IQueryable<int> getLoginStatus(string emailId, string password) { return (from r in taxidb.Registrations where (r.EmailId == emailId && r.Password == password) select r.UserId); } How to return UserName which is a string along with UserId... Any sugge...

Should repositories expose IQueryable to service layer or perform filtering in the implementation?

I'm trying to decide on the best pattern for data access in my MVC application. Currently, having followed the MVC storefront series, I am using repositories, exposing IQueryable to a service layer, which then applies filters. Initially I have been using LINQtoSQL e.g. public interface IMyRepository { IQueryable<MyClass> GetAll(); } ...

Linq In Clause & Predicate building

I have two tables. Report and ReportData. ReportData has a constraint ReportID. How can I write my linq query to return all Report objects where the predicate conditions are met for ReportData? Something like this in SQL: SELECT * FROM Report as r Where r.ServiceID = 3 and r.ReportID IN (Select ReportID FROM ReportData WHERE JobID LIKE...

LinqToSQL not updating database

Hi. I created a database and dbml in visual studio 2010 using its wizards. Everything was working fine until i checked the tables data (also in visual studio server explorer) and none of my updates were there. using (var context = new CenasDataContext()) { context.Log = Console.Out; context.Cenas.InsertOnSubmit(new Cena() { id ...

How to Link VS2010 Database Project and LINQ to SQL

As I am working with the new database projects in VS2010, and as I am learning LINQ to SQL, I am curious as to the best way to link the two groups of information so that when I update one, the other updates along with it. From my research here at SO, as well as in Google, it appears the general rule of thumb is: "Build the database, and...

Mixing LINQ to SQL with properties of objects in a generic list

I am trying to accomplish something like this query: var query = from a in DatabaseTable where listOfObjects.Any(x => x.Id == a.Id) select a; Basically, I want to filter the results where a.Id equals a property of one of the objects in the generic list "listOfObjects". I'm getting the error "Local sequence cann...

How to create a class in dbml file dynamically

Hi, I'm using linq to sql and I need to have a class in the dbml file which some of its properties are creating dynamically . Is there any way to have a class in dbml file with some pre defined properties and some dynamic properties . Or is there any way to create a class in dbml file dynamically? Thank you, ...

Linq 'Index was outside the bounds of the array' problem

In trying to setup a unit test for inserting an item into an SQL Server Express (2008) database using C# Linq I've encountered an error that is causing me some trouble. The Linq code is built using Visual Studio 2008. The exception is thrown on a system running Windows XP SP2. The Linq works fine and the record is inserted appropriate...

ASP.NET-MVC Linq2Sql logic to get linked items on condition

I have a subcontract table with a company field. On the company page, I do not want the company to be able to be deleted if it is attached to an active subcontract. I am currently using the following expression to display the delete button. (Doesn't actually delete, just sets company to inactive.) <% if (item.company1.subcontracts.Co...

Encrypt column data with LINQ

I was wondering if there is easy solution to this or I'm stuck with following: When updating DB: dti.Pass = Crypter.Encrypt(dti.Pass); _db.SubmitChanges(); When selecting from DB: Data.DbTableItem dti = _db.Single(a=>a.Id == id); dti.Pass = Crypter.Decrypt(dti.Pass); Meaning - I am not really into writing repetitive code and thi...

Bulk inserting best way to about it? + Helping me understand fully what I found so far

Hi So I saw this post here and read it and it seems like bulk copy might be the way to go. http://stackoverflow.com/questions/682015/whats-the-best-way-to-bulk-database-inserts-from-c I still have some questions and want to know how things actually work. So I found 2 tutorials. http://www.codeproject.com/KB/cs/MultipleInsertsIn1dbT...