linq-to-sql

C# LINQ equivalent of a somewhat complex SQL query

So I have a SQL Query as Follows SELECT P.Date, P.CategoryName, P.ProductName, SUM(Quantity) Quantity, SUM(Sales) TotalSales, IsLevelThree FROM Products P LEFT JOIN LevelThreeTracking LTT ON P.Date = LTT.Date AND P.CategoryName = P.CategoryName AND P.SecurityID = LTT.SecurityID WHERE P.Date = '12-31-2007' AND P.CategoryName= 'CategoryN...

Problem with the return type of a stored procedure

i have a stored procedure that creates a table then fills this table somehow from tables of the database then selects everything from this table then drops this table.the problem is,how can i use the selected columns from that dropped table im using DataContext i always put the result of the the stored procedure in a list of the type of ...

Stop LINQ to SQL from executing select statements after insert

I'm using LINQ to SQL to update my database. I'm inserting a lot of records, and when I call SubmitChanges(), LINQ to SQL executes an insert and a select statement for each object. I don't really care to update my objects after they are inserted into the database. Do you know I can prevent LINQ to SQL from issuing the select statement...

Linq to SQL: order by value in related table

I have 2 tables which in simplified form look like this: Products( id: int, name: varchar ); ProductSpecs( product_id: int, spec_name: varchar, spec_value: int ); Now I need to sort products (in linq to sql) by value of some specification item (eg. "price"). So I do something like this var products = from p in db.Prod...

Way to Automatically Add a LinqToSql base class to Entities?

Is there an automatic way to add base classes to Linq2Sql entities? I know I can define a partial and implement that but there has to be an automated way, right? ...

LINQ to SQL, Stored Procedure problem handling an INT32 field that can also be NULL

I have a stored procedure that returns two int Values. The first can not be null but the second can actually return a null value. When I try to execute my stored procedure using LINQ to SQL it fails with the statement: "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type." I tried t...

LINQ sorting anonymous types?

How do I do sorting when generating anonymous types in linq to sql? Ex: from e in linq0 order by User descending /* ??? */ select new { Id = e.Id, CommentText = e.CommentText, UserId = e.UserId, User = (e.User.FirstName + " " + e.User.LastName).Trim()), Date = string.Format("{0:d}", e.Date) } ...

Use Linq to Query for terminal/Leaf nodes in Hierarchical Table/Composite pattern

I have a self-referencing table with an Id, CategoryName, and ParentId. It's a typical scenario of a hierarchy table of categories that can themselves be divided into categories that DB experts tell me is called the adjacency model. What I want is to use Linq to SQL to query for subcategories that themselves are related to no other sub...

DataContext naming Best Practices

Are there any? Do you give the DataContext the name of the Database you are getting data from or.. ...

What's the simplest way to display NULL values as "NULL" with WPF Data Binding?

I have this legacy database for which I'm building a custom viewer using Linq to Sql. Now some of the fields in a table can have the value NULL. Using normal databinding in a DataTemplate (typed for the Class generated by the ORM Designer) <TextBlock Text="{Binding Path=columnX}"/> If columnX has value NULL, nothing is displayed. (It...

How do I insert entities with Linq To SQL where I wish to specify their identities - eg, pre-populating tables on DB creation?

I'm engaged in writing a product using LinqToSql for data-access. While we're rapidly developing, the model changes often. This means we dump and reload the database lots while developing. Thus, overhead is involved to recreate baseline data each time. A process that restores a DB backup is not useful, because we would need to update...

What is the best way to build a data layer across multiple databases?

First a bit about the environment: We use a program called Clearview to manage service relationships with our customers, including call center and field service work. In order to better support clients and our field technicians we also developed a web site to provide access to the service records in Clearview and reporting. Over time ...

Lost decimal precision and scale using LINQ and stored procedure with output parameters

I have a stored procedure that uses output parameters like this: ALTER PROCEDURE [GetAmount] ( @orderID [int], @totalcost decimal(18,2) OUTPUT ) SELECT @totalcost = cost FROM mytable WHERE orderID = @orderID When I drag the stored procedure onto the designer, the resulting code in the designer.cs file ends up losing the precisi...

Why do I need Stored Procedures when I have LINQ to SQL

My understanding of Linq to Sql is it will take my Linq statement and convert it into an equivalent SQL statement. So var products = from p in db.Products where p.Category.CategoryName == "Beverages" select p Just turns into Select * from Products where CategoryName = 'Beverages' If that's the case, ...

how to do subquery in LINQ

Here's an example of the query I'm trying to convert to LINQ: SELECT * FROM Users WHERE Users.lastname LIKE '%fra%' AND Users.Id IN ( SELECT UserId FROM CompanyRolesToUsers WHERE CompanyRoleId in (2,3,4) ) There is a FK relationship between CompanyRolesToUsers and Users, but it's a many to many relatio...

TDD, What are your techniques for finding good tests?

I am writing a simple web app using Linq to Sql as my datalayer as I like Linq2Sql very much. I have been reading a bit about DDD and TDD lately and wanted to give it a shot. First and foremost it strikes me that Linq2Sql and DDD don't go along too great. My other problem is finding tests, I actually find it very hard to define good te...

Maximum number of columns in a LINQ to SQL object ?

I have 62 columns in a table under SQL 2005 and LINQ to SQL doesn't handle the updates though the reading would work just fine, I tried re-adding the table to the model, created a new data model but nothing worked, I'm guessing I've hit the maximum number of columns limit on an object, can anyone explain that ? ...

Is it possible to reverse the Uni-Directional behavior of a DataContractSerialization of a Linq 2 SQL object graph?

"By convention, the property on the parent side of a primary-foreign key relationship is marked for serialization. The other side in a bidirectional association is not serialized." Is it possible to somehow reverse this so that the child side serializes the parent instead? ...

linq2sql: Cannot add an entity with a key that is already in use

I have a linq2sql setup where objects are sent from client side (flex via flourinefx) and attach them to a new datacontext a seen below: I also have a "global" datacontext that is used throughout the session. public static void Update(Enquiry enquiry) { OffertaDataContext db = new OffertaDataContext(); db.Enq...

LINQ to SQL Query Syntax

I have several tables that center around an organization table that simply holds a unique ID value. Each Organization can then be at a particular location and have a particular name. The tricky part is that the organizations support location and name changes with a specified effective date of each change. For this example I have 4 releva...