linq-to-sql

LINQ to SQL -DAL

I wish to Implement LINQ-to-SQL as my Data Access Layer. I am using SQL Server. Is there any Linq-to-SQL framework (commercial or open source) available that can function like Application Block (caching block, validation, etc..)? ...

MVC/jQuery/LINQ-to-SQL: Delete with confirmation and redirect on success

Here's the behavior I'm looking for: User selects a record from an index view, which redirects to that record's detail view. User clicks a link to delete the currently displayed record. A modal dialog asks for confirmation of the delete. User presses the Confirm button. The associated controller action method is executed, which is supp...

How to flatten Linq-To-Sql table mappings?

I have a Linq-To-Sql mapping between POCO classes and my DB. I want to be able to add properties on my classes which represent slightly more complex constructs than simple scalr values. For instance, I have a custom struct type which holds two simple scalar values. I don't want to make this another table and then add a FK as the propert...

Linq-to-SQL throwing NullReferenceException despite checking for null arguments

I'm trying to write Linq-To-SQL queries in LinqPad to help migrate users from old tables to new tables. Part of this migration is storing all addresses in a separate table. I'm using the following query to determine if a user's address exists in the new table (so I don't have duplicate entries): var addresses = from a in Addresses where...

ASP.Net MVC HABTM Association

I must be missing something, when trying to define a HABTM association with 2 of my models. I have a "Product" table, and a "Category" table, and a "ProductsCategories" join table. In SQL Server, I am defining the Relationship between the tables on the join table. However, when I create the LINQ to SQL model "Product," I get "Product....

Why does do this code do if(sz !=sz2) sz = sz2 !?!

For the first time i created a linq to sql classes. I decided to look at the class and found this. What... why is it doing if(sz !=sz2) { sz = sz2; }. I dont understand. Why isnt the set generated as this._Property1 = value? private string _Property1; [Column(Storage="_Property1", CanBeNull=false)] public string Property1 { get ...

What class to return from LINQ to SQL classes when using JOIN

I am returning List from my BusinessLogic methods where the Type is one of the classes generated by Linq2Sql classes. Now if I use a join statement and get a result containing properties(columns) from different tables, then my return object should be a List<> of what? I am using asp.net ajax components from Telerik and set the datasour...

LINQ to SQL: Data from two unrelated tables, sorting on date

I have two tables tblActionLog and tblLoginLog. This is tblActionLog: tblActionLog ------------ ID (int) Happened (DateTime) ...etc. This is tblLoginLog: tblLoginLog ----------- ID (int) LoginDate (DateTime) ...etc. Now I want to have a GridView with information from both of these tables interleaved in eachother so that their sepa...

How to tell if a Linq object has been attached to a data context?

Using Linq-to-Sql: MyClass obj; ... // need to delete this object dataContext.GetTable(obj.GetType()).DeleteOnSubmit(obj); BUT I don't know whether or not obj has been attached to the data context. And if it hasn't, that last call to DeleteOnSubmit throws an exception. There has to be an easy way of telling whether obj is attached ...

Query with multiple wheres

I need to write a query that will perform a keyword search on a database table. The code currently looks something like this (albeit with a hard-coded set of keywords): var keywords = new [] { "alpha", "bravo", "charlie" }; IQueryable<Story> stories = DataContext.Stories; foreach( var keyword in keywords ) { stories = from story i...

ASP.Net MVC Error Validation - How to display validation message when passing a custom view model to a view

I have been adding error and business validation to my app, and when I test using a view that was strongly typed to one model, let's say locations, I get the validation summary as well as the validation messages for each field that didn't pass, plus my css highlights the appropriate field as expected... ...when I try this with a view th...

LINQ to SQL sum of decimal values

I can't figure out how to do a simple sum of decimal values. Table<StaffTime> times = ctx.GetTable<StaffTime>(); var query = from t in times select new { t.Hours.Sum() } Isn't Sum an extension method? What am I missing? Bob ...

EntityRefs and EntitySets - When are they retrieved?

Assume I have an entity named "A" that contains an EntityRef and an EntitySet. I'm curious as to exactly when, by default, the EntityRef and EntitySet are retrieved when "A" is retrieved from the database? It seems, from my experimentation, that the EntityRef is retrieved at the same time entity "A" is retrieved from the database and the...

LINQ to SQL - Format a string before saving?

I'm trying to convert an existing (non-LINQ to SQL) class into a LINQ to SQL entity class which has an existing (db column) property like: public string MyString { get { return myString; } set { myString = FormatMyString(value); } } Is there a way to do this sort of processing on the value of entity class property before savin...

Linq to sql type comparison

I have a varialbe name age and that's type is int? My linq to sql query is: from t in db.Profiles where t.age == age select t having a comparison between int and int? at where clause. how the query processor of linq to sql will process in this case? ...

Linq: How to find rows searching in two columns with the same criteria

Hello, In my controller, I've this action method called "SearchForContact," which takes 2 strings parameters firstName and LastName. There are 3 situations: If both parameters are null, the view is redisplayed with an error message, prompting the user to enter at least one of the 2 parameters if both parameters are not null, I can ret...

Need SELECT WHERE COUNT = INT in LINQ

I have the following table that records when a particular room in a hotel (designated by a three character code [dlx, sup, jac, etc..]) is sold out on a particular DATETIME. CREATE TABLE [dbo].[RoomSoldOut]( [SoldOutID] [int] IDENTITY(1,1) NOT NULL, [RoomType] [nchar](3) NOT NULL, [SoldOutDate] [datetime] NOT NULL, CONSTRAI...

Linq group by and count - how do I add more columns?

I have the following linq query from c in AllContracts group c.StatusId by c.StatusDescription into g select new {StatusDescription = g.Key, CountOf = g.Count()} which returns StatusDescription CountOf End date has passed 1 Suspended 1 Setup phase 2 Running 7 However I would love ...

ASP.Net User activity tracking in database

This is about a simple yet efficient activity logging framework that I want to integrate with my existing ASP.Net based web-app (I've a LINQ-to-SQL based SQL DB as backend). I'm using something like a service-architecture to perform DB operations - that is invoke relevant LINQ operations. I've a service class for almost every entity (i.e...

Strange LINQ to SQL behaviour (BUG?)

We have this very strange issue on LINQ to SQL Code. Consider this code snippet : var wissen = db.Table.Where(f => f.Name == somevalue); db.Table.DeleteAllOnSubmit(wissen); db.SubmitChanges(); This works as expected on our dev servers, but when we are deploying this to our production server it doesn't give any errors but it doesn't ...