linq-to-sql

Group By store procedure with Linq to Sql

I have this store procedure... select a.IsDirectClient from dbo.Companies as a where a.SchemeName = @SchemeName group by a.IsDirectClient which I'd like to use in my solution via linq and a data context. However I can't drop this store procedure onto the company table because I get the "... database objects return a sch...

asp.net mvc linq sql problem

Hi all, I am working on a project using asp.net mvc 2 and linq to sql. The problem occurs when trying to insert data into a table where the table has an identity column type of tinyint. When trying to insert the following error occurs: The primary key column of type 'TinyInt' cannot be generated by the server. Does linq to sql suppor...

linq-to-sql "an attempt has been made to attach or add an entity that is not new"?

I've been getting several errors: cannot add an entity with a key that is already in use An attempt has been made to attach or add an entity that is not new, perhaps having been loaded from another datacontext In case 1, this stems from trying to set the key for an entity versus the entity. In case 2, I'm not attaching an entity but ...

Which is more flexible in regards to database changes, EF 4 or Linq to SQL

Reading about both Linq to SQL and Entity Framework I have developed the impression that EF is more suitable for apps that get data from multiple data sources. But as I am reading about MVC2 models I see an example where EF is more loosely coupled with your data model. If I have to add or remove some columns from a table then what is i...

Linq to SQL System.InvalidOperationException : Member AutoSync failure

Hi, I've got a couple of Linq to SQL entities which are causing me problems: [Table(Name = "ViewName")] [InheritanceMapping(Code = false, Type = typeof(Entity1), IsDefault = true)] [InheritanceMapping(Code = true, Type = typeof(Entity2))] public class Entity1 { [Column(AutoSync = AutoSync.OnInsert, DbType = "uniqueidentifier NOT NULL",...

Is Entity Framework more mockable than Linq2Sql?

I've found that Linq2Sql doesn't (Rhino) mock well, as the interfaces I need aren't there. Does EF generate code that's more mockable? NOTE: I'm not mocking, yet, without interfaces, the next reader of this question may not have my bias. EDIT: VS2008 / 3.5 for now. ...

LINQ Query Help!

Can someone hep me converting the following SQL query into LINQ? select convert(varchar(10),date,110) as 'Date', max(users) as 'Maximum Number of Users', max(transactions) as 'Maximum Number of Transactions' from stats where datepart(Year, Date) = '2010' group by convert(varchar(10),date,110) order by conv...

How to perform Linq select new with datetime in SQL 2008

In our C# code I recently changed a line from inside a linq-to-sql select new query as follows: OrderDate = (p.OrderDate.HasValue ? p.OrderDate.Value.Year.ToString() + "-" + p.OrderDate.Value.Month.ToString() + "-" + p.OrderDate.Value.Day.ToString() : "") To: OrderDate = (p.OrderDate.HasValue ? p.OrderDate.Value.T...

Is this ok in a DI world ?

Hello Stackers :) I have implemented the Unity DI in a project of mine, but I have, what I believe is a simple question. My DataContext: public partial class AuctionDataContext : DataContext { public Table<AuctionItem> AuctionItems; ... } Some code for inserting an AuctionItem in the database. Please notice how I cast the inte...

How do you cast a LinqToSql Table<TEntity> as a Table<IEntity> where TEntity : IEntity?

I'm trying to use DbLinq with a SQLite database, but I'm running into a problem when I try to cast an ITable as a Queryable<TEntity>. There is a known bug in DbLinq (Issue 211), which might be the source of my problem, but I wanted to make sure my code is sound and, if it is, find out if there might be something I can do to work around ...

Linq to SQL - Default Insert/Update/Delete functions not present

Using VS2010 and SQL express 2005, Ive created a database, created a simple table, added the database to VS2010, dragged the table i wish to query into a new 'Linq To SQL' class. Now, selecting works as expected - totally great. However it has not generated the Remove() method, mydb.SumbitChanges() does nothing after amending a retriev...

LINQ Option Missing in Data Source for Grid View

Hi, I don't see a LINQ option when picking a data source for a grid view - any ideas? I see the other options like SQL etc. If I go into a page in another folder, I'm able to see this option. I just upgraded to Visual Web Developer 2010. Thanks! ...

Populate JOIN into a list in one database query

I am trying to get the records from the 'many' table of a one-to-many relationship and add them as a list to the relevant record from the 'one' table. I am also trying to do this in a single database request. Code derived from http://stackoverflow.com/questions/1580199/linq-to-sql-populate-join-result-into-a-list almost achieves the in...

how to convert Database Hierarchical Data to XML using ASP.net 3.5 and LINQ

hello guys! i have a table with hierarchical structure. like this: and table data shown here: this strategy gives me the ability to have unbounded categories and sub-categories. i use ASP.net 3.5 SP1 and LINQ and MSSQL Server2005. How to convert it to XML? I can Do it with Dataset Object and ".GetXML()" method. but how to implement ...

Convert var to DataTable

I have var item which I want to convert in to a Datatable. How can I do this. var items = (from myObj in this.Context.Orders group myObj by myObj.OrderDate.ToString("yyyy-mm") into ymGroup select new { Date = ymGroup.Key, Start = ymGroup.Min(c => c.OrderId), End = ymGroup.M...

Linq2SQL vs NHibernate performance (have I gone mad?)

I have written the following tests to compare performance of Linq2SQL and NHibernate and I find results to be somewhat strange. Mappings are straight forward and identical for both. Both are running against a live DB. Although I'm not deleting Campaigns in case of Linq, but that shouldn't affect performance by more than 10 ms. Linq: ...

how to bind gridview through linq

I am using linq to sql. Currently i am binding gridview through linq which query written in business logic call.I have extract record through query in business logic class and i want to bind that particular data to gridviw and return data. How to retrun data which type is array? The code is mention below: CMSBusiness.DataClasses1DataC...

Linq to SQL - what's better?

db.Albums.FirstOrDefault(x => x.OrderId == orderId) or db.Albums.FirstOrDefault(x => x.OrderId.Equals(orderId)) ...

How to get the last element by date of each "type" in LINQ or TSQL

Imagine to have a table defined as CREATE TABLE [dbo].[Price]( [ID] [int] NOT NULL, [StartDate] [datetime] NOT NULL, [Price] [int] NOT NULL ) where ID is the identifier of an action having a certain Price. This price can be updated if necessary by adding a new line with the same ID, different Price, and a more recent date....

Why are my bound parameters all identical (using Linq)?

When I run this snippet of code: string[] words = new string[] { "foo", "bar" }; var results = from row in Assets select row; foreach (string word in words) { results = results.Where(row => row.Name.Contains(word)); } I get this SQL: -- Region Parameters DECLARE @p0 VarChar(5) = '%bar%' DECLARE @p1 VarChar(5) = '%bar%' -- EndRegi...