linq-to-sql

Can LINQ2SQL return a few columns or is it entity or single column only?

Hi, From what I understand, using something like nHibernate will return either a single result like int (say returning a count) but it can't return say 2 columns or 3 columns from the users table. Is linq the same way? e.g. say I have the Users table with columns: UserID, username, password, email, datecreated) Could it return User...

Should the LINQ datacontext be stored in request.items for web apps?

Should the LINQ datacontext be stored in request.items for web apps? ...

Linq2Sql -> Searching the database against a local collection of values - "Queries with local collections are not supported"

I am running up against the wall with Linq2SQL. I love it, its amazing the flexibility, but I have run up against interesting hangups. I hope its just my lack of knowledge of it, and there really is a solution. Take for example... a linq2sql query like this: // some local collection of ids var terminalID = new List<int>(){1, 2, 3, 4, ...

Mimicking SQL Insert Trigger with LINQ-to-SQL

Using LINQ-to-SQL, I would like to automatically create child records when inserting the parent entity. Basically, mimicking how an SQL Insert trigger would work, but in-code so that some additional processing can be done. The parent has an association to the child, but it seems that I cannot simply add new child records during the Dat...

LINQ to SQL: Fake Repository with Many-to-Many Relationships?

I have two tables, Clients and Administrators, which are linked in a many-to-many relationship by the table ClientAdministrators. In the real application this works fine and I can get a list of Administrators for my Client. My problem comes in trying to unit test the service class that is retrieving this from the repository. I have a ...

MSLinqToSQLGenerator generates different output than SQLMetal

I have a rather large DBML file and recently discovered that Visual Studio's MSLinqToSQLGenerator is generating different output than: SqlMetal.exe All.dbml /code:All.designer.vb /namespace:LINQ2FSE /pluralize /provider:SQL2005 It seems to have dropped an arbitrary (and I think relatively small) set of associations from the generated ...

If I inner join in a linq-to-sql, what kind of a return type do I get?

Hi, Say I have a Article object, and a Category object. The article object has a CategoryID property. With linq-to-sql, how would I get list of articles and its associated category? What will the return type be? ...

Convert a Sql query with group by clause and/or aggregate function into linq to sql?

I have a table Auditing logins, I'd like to just extract the latest login for each user, which the below sql accomplishes. How would I format the sql query into a linq to sql query? SELECT * FROM AuditTable adt1 inner join UserTable usr on adt1.[UserName] = usr.[User_Id] WHERE [TimeStamp] = ( SELECT MAX([TimeStamp]) ...

MVVM & WCF - View Model and Model Relationship

I am not understanding how my model can be a WCF service. It makes sense when its an Astoria partial class residing on the client that allows remote calls to do persistence calls, but a WCF service doesn't have properties for model fields that can be used to update a data store. Even if I could factor out an interface for a model/dom...

Linq to Sql binding problem.

Hello I have a problem for binding to gridview... Assume I have two table : Album and Photo each has 'Name' column (Assume they are the same name). When I bind the Photo using Linq to Sql, I can reference the data like this DataBinder.Eval(Container.DataItem, "Name"); but if I want to have the Name of the Album is it possible to fetch ...

Linq2Sql: How to handle tables with the same name and different schema names

I have a database with multiple tables which have the same name but are from different schemas. For example: [DatabaseName].[Schema1].[MyTable] [DatabaseName].[Schema2].[MyTable] When Linq2Sql generates code for this database, it appears to just be picking up the table from the first schema and completely ignoring the second schema: ...

Linq to SQL dataContext life-time problem

Hi, I'm using Linq to SQL to write several unit tests. Sometimes I have code something like this: var products = dataContext.Products; Assert.That(1, dataContext.ExecuteQuery<int>("select count(*) from product").First()); //this works Assert.That(1, products.Count()); //this works dataContext.spCalculateMoreProducts(); Assert.That(2, ...

LINQ to SQL and SQL 2000 - Extensibility Method Definitions gone

I have just had to use LINQ to SQL on a SQL 2000 database and I have noticed that it does not include all the "Extensibility Method Definitions" actions, why is this? ...

Determine primary key using LINQ to SQL

I'm writing a LINQ to SQL based repository where I want to allow a GetByID with an int parameter. The signature is: public T GetByID(int id) { // Return return _dataContext.GetTable<T>() ....; } My tables have different names for the primary key. What I'd like to do is determine dynamically for each T what the primary key ...

Insertion conflict LINQ-to-SQL

I get this error when insert a new album: {System.Data.SqlClient.SqlException: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_ChannelAlbum_Group'. The conflict occurred in database 'Stamper', table 'Channel', column 'ID'. I don't know what is going on because sometime I insert the new album to the context the er...

Linq-to-Sql NotSupportedException: Can I be sure it will work on a newer server version ?

I am doing a small reporting project that needs to read a lot of data from an SQL Server 2000 database. I am using Linq-to-SQL; and when I run one of my queries, I get this Exception: NotSupportedException: Cannot translate expression to SQL for this server version This message indicates that my query will work on a newer server versi...

Whats the Efficient way to get data from db using LINQ To SQL or LINQ To Entities?

When you run Linq to Sql or Linq to Entites to get a list of records it runs query to select all fields from a table. Is it an efficient solution. Lets say: I run this LINQ dim lstCustomers = from c in db.Customers select c it run query to get all fields from a table whether i need all fields or not. I am using asp.net with MVC so sho...

ASP.NET DynamicData - add table at runtime?

We currently have an ASP.NET Dynamic Data website for our intranet that was developed by a contractor who is no longer with us. None of our developers currently have VS 2008 installed, so are not able to open modify the source code of this intranet site to add 2 new tables to the site. The new tables require simple CRUD operations exac...

linq to sql linking objects by multiple ID fields

Im working on a project at work which is pretty much a commissioning manager and i cant seem to figure out how to link the Items table properly in linq to sql. I have a package detail table that has (among other things) DepartmentID, CategoryID, ItemID And the Items table (is actually a database view as its from a different database ...

LINQ to SQL validate all fields, not just stop at first failed field

Hello, I just started using LINQ to SQL classes, and really like how this helps me write readable code. In the documentation, typical examples state that to do custom validation, you create a partial class as so:: partial class Customer { partial void OnCustomerIDChanging(string value) { if (value=="BADVALUE") throw new...