linq-to-sql

LinqToSql static DataContext in a web application

In a web application that I have run across, I found the following code to deal with the DataContext when dealing with LinqToSQL public partial class DbDataContext { public static DbDataContext DB { get { if (HttpContext.Current.Items["DB"] == null) HttpContext.Current.Items["DB"] = new DbDataCont...

ASP.NET MVC Model & Business Objects

I am looking for some guidance on how to incorporate business rules into an asp.net mvc application and how they relate to the model. First a little background so we know what kind of solutions are relative for this question. At work we use WinForms, MVP, BusinessObjects, DataAccessObjects, and DataTransferObjects. The boundaries of...

OnValidate() and LINQ to Entities

I would like to implement some business rule validation like Scott Guthrie did in his MVC Nerddinner tutorial (http://nerddinnerbook.s3.amazonaws.com/Part3.htm), but I'm running into a problem trying to do so. Scott was using Linq to SQL in his tutorial. He creates partial classes for his data objects, and then he implements a partial ...

LINQ to SQL - How to deal with changes to database

I'm fairly new to LINQ to SQL, so I could be missing something basic here. I created a LINQ to SQL layer, generated all the dbml files etc., and created a LINQ query which worked fine. I then made a change to the database, and wanted to get that change reflected in the ORM layer. To do this, I deleted my ORM layer and created a new on...

Regular Expressions in C#

I'm trying to do datacontent.executeCommand("string") in C#. When I am trying to do Insertion I am having trouble because everytime there is a 'word' in a entry I thinks it's a new column, and i throws an exception. How do you turn the 'word' into ''word''? ...

Recreate Linq to Sql code when table changes where customised

I have a Linq to Sql Model generated by VS ORD, but I want to make a changes to it to work around a couple of issues. I can change the designer.cs generated code , but if I ever need to recreate a table within the model, then I delete it and drag it back to the ORD. This however loses all of my customisations. Is there another way? I ha...

Query a recordset from an existing query Linq To Sql

I'm in a little bit of a road block here, but what I would like to ultimately do is to create a recordset based off of a query and store the information into separate objects (let's call them Foo) then create a new query to group all the Foo objects with the same id's into an ArrayList into Bar objects. How would I go about doing this in...

Deleted Rows Remain in my Data Context Unless I Initialize It Again

I have a datacontext say myDataContext, it has on several classes (tables) and stored procedures, one of which deletes directly from the database (the logic requires this), however, the deleted rows remain in my myDataContext unless I initialize it again. Is there a way to avoid this? The stored procedure removes the record from the data...

LinqToSql and Stored Procedure question

Hi folks, I have a stored procedure that returns a number of fields, pew row. Is there anyway i can define the class which the stored procedure 'goes into', instead of it using the auto-generated class? The class i want the results to populate is a POCO, not another LinqToSql table. Is this possible? i'm assuming that the poco class p...

MySQL Get All Except If

I have a MySQL table and I want to extract all rows except if the column contains a specific value. Is this possible? My table structure +----+------+------+------+ | ID | col1 | col2 | col3 | +----+------+------+------+ | 1 | blah | blah | boo | +----+------+------+------+ | 2 | blah | blah | blah | +----+------+------+------+ | 3...

How do you do a join in LinqToSQL?

I have a data setup of the following form: State 1->n County 1->n City. Within my State object, I want to return all counties that contain at least one city with a population greater than p. Were I to write this in sql it would be: select distinct co.* from County co join City ci on ci.CountyID = co.ID where ci.Population > @p and co....

Programmatic control of the Delay Loaded property in LINQ to SQL

Using LINQ to SQL, is there any way to specify "Delay Loaded = true" for some properties on entities using code? I can do it manually in the designer but I will lose that customization if the table is updated/rebound. I know of DataLoadOptions and LoadWith(), but that's for using eager loading instead of lazy loading, and I want to spe...

LINQ query help (LINQ to SQL)

Hi, I'm not really familiar with LINQ, but I'm currently working on an ASP.NET project and using LINQ to SQL as ORM (I'm building a route planning application). I have a problem creating a query the way I need it, so maybe anyone could give me a hint how to achieve this. My current query looks like this: var results = from lift in db.L...

Linq Compiled Query using Contains (Like SQLs IN statement)

I'm in the process of speeding up Linq to SQL queries in a web app by converting them to Compiled queries and I ran into a problem converting a very simple statement. I want to compile a simple statement that uses the following parameters to get valid employees from the database: TestParams myParams = new TestParams { ValidEmps = n...

LINQ: No Translation to SQL

Can you explain why example #1 fails with "No Translation to SQL" and example #2 works fine? All in Repository: EX#1: public DomainPerson GetBestPerson() { var person= GetPeople().Where(p=>p.Quality=="Best").SingleOrDefault(); return person; } public IQueryable<DomainPerson> GetPeople() { var people= from ...

Convert Linq Query Result to Dictionary

Hi, I want to add some rows to a database using Linq to SQL, but I want to make a "custom check" before adding the rows to know if I must add, replace or ignore the incomming rows. I'd like to keep the trafic between the client and the DB server as low as possible and minimize the number of queries. To do this, I want to fetch as little...

Can I use the NerdDinner sample project as a base template for a larger project?

I'm new to the MVC framework and have just run through the NerdDinner sample project. I'm loving this approach over form-based asp.net. I'd like to spin of a more sizable side project using this same approach. Do you see anything in that project that would prevent me from enlarging the basic structure to a more complex website? Exa...

Handling different databases with LINQ based off the user which is current logged in?

I've been given the task of creating a site that will allow our various large clients to log into our website and click on our various pages to view analytics data based on their sales. Does anyone have any idea about the best way to handle multiple databases based off the user? Lets say we have 3 big name clients, the design decision ...

LINQ To SQL SubSelect Like Query

Hi folks, imagine there are two tables. Order +----------------+ | ID | | Name | +----------------+ OrderStatus +----------------+ | ID | | OrderId | | StatusId | +----------------+ A Order can have more than one OrderStatus, which could be called OrderStatusHistory. I'll have an Strong...

When I make a database change, how do I know what needs to be removed and readded to the LinqToSql O/R Designer in Visual Studio?

For example, I alter a column in a table to be not null from null. I need to then delete and readd the table. That part is pretty clear. What about views, functions, and stored procedures that access the table, especially that column in the table? I've tried running diffs against the files generated by SQLMetal with some limited succ...