linq-to-sql

LINQ to SQL - Select SUM with a WHERE

I'm having trouble getting a Linq to Sql query to work. Basically, I want the sum of a column in a table for only rows matching a certain condition. A simplified version is this: Orders = order that contains products OrderProducts = relational table containing products that are in an order Products = table containing information about...

Mutli tenancy backstop check on Linq-to-sql entity load()

I am writing an aspx application that will host 1000's of small customers in a communal SQL Server database. All entities will be created and loaded via Linq-To-Sql. Surrogate keys (identity columns) will be used throughout the schema for all table relationships and so starting with a root customer object I should be able to navigate to...

InsertOnSubmit not triggering a database insert on SubmitChanges

I'm experiencing an odd scenario and I'm looking for ways to figure out what's going wrong. I've got a piece of code that inserts a row into a table - the kind of thing I've done in dozens of other apps - but the end result is nothing happens on the database end, and no errors are generated. How do I find out what's going wrong? Here's ...

Creating an Extendable Application with LINQ-to-SQL or Entity Framework

I am working on a framework which will provide some basic functionality for a number of applications which our company is going to develop. The framework will come with a basic database schema which will support this functionality. Developers using the framework will be expected to extend the database schema with their own tables. Fut...

Linq2Entities / Linq2Sql - Comprehensive search...

As I read the a similar question about searching SQL I realized I am very curious on how to do the same thing in Linq. Assume a DB Table with the fields "Title", "Tags" (comma separated) and "Body" (HTML) that has been connected to a Linq2[Entities|Sql] How would you go about searching for a string across those fields. Yes, I could b...

datasource to dsiplay in grid; list<ListofCarGroups> where ListofCarGroups is List<ListOfCars>

Hello, I understand that i cannot do something like List<List<string,string>>. So i have to put each object in a class, and then have a list of classes. Below is the problem as I understand it. Class FailedField has properties FieldName, Error sample data: "year", "must be between 1900-2100" Class FailedFields is a list of FailedFiel...

How to force Linq to update last edit time of a row?

In database, I have a LastEditTime column of type datetime. I used this to track last row update/insert time. Linq throws exception claiming that it can not insert null at that column. Naturally, since LastEditTime column is NOT NULL, an exception is expected. My hand generated query inserts getutcdate(). How can I ask Linq to do simila...

Aggregate LINQ results

If I have a Publisher table that has many Books, and each Book can have many Authors, how do I get a list of distinct Authors for a Publisher? In SQL you would just join all the tables, select the Author, and use SELECT DISTINCT. Using LINQ I end up with an IEnumerable(Of EntitySet(of Author)): Dim temp = From p in Publishers Select (Fr...

LINQ to SQL many to many, contains

I have the following DB (simplified) Image - ImageTag - Tag ImageTag is a joining table to form the many to many relationship. I want to make a method which returns all images which contain x tags, this is what I have started with: public static IQueryable<Image> WithTags(this IQueryable<Image> qry, IEnumerable<Tag> tags) { retu...

Asp.net - Trouble with updating LINQ Classes

Hi, We just created a new field in a database table, and so deleted, and re-inserted the table in the LINQ Class. The new database field appears in the LINQ Class in the diagram. However, when we're using the field, we get an error that says the table does not contain a definition for the field. Any ideas on how we can solve this? Than...

Linq to Sql Add Child records

I have two entities in Linq to Sql setup as a Parent/Child relationship (Parent can have many Children). I have an existing parent record and trying to create new child records to be "added" to the parent. Let's use the familiar Order and OrderDetails model for explanation. I am trying a bunch of things to add my "child" to the parent...

Random LINQ to SQL records w/ "int" primary keys

How do I get a 5 random records from a LINQ to SQL Table[T]? All the examples I've found via google use uniqueidentifier as the primary key http://www.davidmuto.com/Blog.muto/View/random-records-with-linq-to-sql Is there a way to do this when the primary key is an auto-incrementing integer? Some records can be deleted too, so the min...

ADO.NET Entity Framework Custom SQL Expressions for Inserts/Updates/Deletes

Hi, In ScottGu's blog, he shows how to override methods of Linq to SQL classes so that modifications to data can be overriden in .NET side customly. http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx (Custom SQL Expressions for Inserts/Updates/Deletes part) Is there a way to acco...

LINQ to SQL blurs seperation of concern?

I'll just put it out there that I'm new to LINQ to SQL. Hell, i'm relatively new to programming in general. Anyways, I would like to learn and use LINQ to SQL to a project that was built using .NET 2.0 Framework. The project uses stored procedures to access the database (there's no dynamic SQL queries on the front end servers). LINQ to ...

Look-up tables with Linq-to-Sql

Hi all, I'm working on a car dealer website at the moment, and I've been working on a 'Vehicle' model in my database. I've been making extensive use of lookup tables with FK relationships for things like Colour, Make, Model etc. So a basic version could be something like: Vehicle { Id MakeId ModelId ColourId Price ...

Inner queries in LINQ

Hi, I have the folowing SQL query: Select * from aspnet_Users where UserId Not in (select UN.ConnectedToUserID from tblUserNetwork UN ) and UserId <> '82522f05-2650-466a-a430-72e6c9fb68b7' What will be the LINQ equivalent to this SQL. Thanks ...

Manually select related table data (SELECT N + 1 problem) LINQ to SQL

Database example: Image - ImageTag - Tag Images can have multiple tags. The relationships are set up fine and stuff but I am running into performance issues. I have many different queries which select Images according to different criteria. They work fine, however the data for the Tags are not selected with these queries. This means ...

IEnumerable.Except() and a custom comparer

Hello, I'm having troubles with the Except() method. Instead returning the difference, it returns the original set. I've tried by implementing the IEquatable and IEqualityComparer in the Account. I've also tried creating a seperate IEqualityComparer class for Account. When the Except() method is called from main, it doesn't seem to ca...

Updating table with composite primary key using LINQ

Hi, I have a table named tblUserNetwork. It contains composite primary key on UserID and ConnectedToUserID When i update the table networks.Update(networks), it gives error like this: Table dbo.tblUserNetwork contains a composite primary key field What is the problem in updating. I am updating many other table they don't have comp...

Does LINQ to SQL support the t-sql "in" statement

I dont't know how to describe the title better (so feel free to change) but essntial I want to produce the equivilent of the following statement in LINQ to SQL: select * from products where category in ('shoes','boots') The fields will be coming from the query string essentially (more secure than this but for ease of explanation) i.e ...