linq-to-sql

Updating multiple tables at the same time in Linq-to-SQL

How do I update two tables at the same time using Linq-to-SQL? var z = from a in db.Products join b in db.ProductSubcategories on a.ProductSubcategoryID equals b.ProductSubcategoryID join d in db.ProductCategories on b.ProductCategoryID equals d.ProductCategory...

Using one LINQ statement with different parameters

I have a pretty complex linq statement I need to access for different methods. Each of these methods may need to see the resulting data with different parameters. For one method it may be a project code, for another it may be language. The statement is pretty much the same it's just the where part which changes. I have not been abl...

Adding more attributes to LINQ to SQL entity

I want to add browsable attribute to some properties for entities generated by LINQ to SQL. Is it a good idea? Since these entities are auto-generated, and when I regenerate they (the attributes I have added) might be overwritten. ...

Need advice on comparing the performance of 2 equivalent linq to sql queries

I am working on tool to optimize linq to sql queries. Basically it intercepts the linq execution pipeline and makes some optimizations like for example removing a redundant join from a query. Of course, there is an overhead in the execution time before the query gets executed in the dbms, but then, the query should be processed faster. I...

What constitutes explicit creation of entities in LINQ to SQL? What elegant "solutions" are there to this limitation?

Hi SO, I've been having problems with the rather famous "Explicit construction of entity type '##' in query is not allowed." error. Now, for what I understand, this exists because if explicit construction of these objects were allowed, tracking changes to the database would be very complicated. So I ask: What constitutes the explic...

Can I force the auto-generated Linq-to-SQL classes to use an OUTER JOIN?

Let's say I have an Order table which has a FirstSalesPersonId field and a SecondSalesPersonId field. Both of these are foreign keys that reference the SalesPerson table. For any given order, either one or two salespersons may be credited with the order. In other words, FirstSalesPersonId can never be NULL, but SecondSalesPersonId can be...

Can I use the auto-generated Linq-to-SQL entity classes in 'disconnected' mode?

Suppose I have an automatically-generated Employee class based on the Employees table in my database. Now suppose that I want to pass employee data to a ShowAges method that will print out name & age for a list of employees. I'll retrieve the data for a given set of employees via a linq query, which will return me a set of Employee inst...

How can I use a compound condition in a join in Linq?

Let's say I have a Customer table which has a PrimaryContactId field and a SecondaryContactId field. Both of these are foreign keys that reference the Contact table. For any given customer, either one or two contacts may be stored. In other words, PrimaryContactId can never be NULL, but SecondaryContactId can be NULL. If I drop my Custo...

Linq left joining with non trivial condition

This is fine, it produces a left join var q = from c in categories join p in products on c equals p.Category into ps from p in ps.DefaultIfEmpty() select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName }; But what about if I wanted to do something like this: ... on p.date between c.s...

Linq to SQL - design question.

HI, Currently i have one big datacontex with 35 tables (i dragged all my DB tables to the designer). I must admit it is very comfortable cause i have ORM to my full DB and query with linq is easy and simple. My questions are: 1. Would you consider it bad design to have one datacontext with 35 tables or should i split it to logic units?...

Unable to add item to dataset in Linq to SQL

I am having an issue adding an item to my dataset in Linq to SQL. I am using the exact same method in other tables with no problem. I suspect I know the problem but cannot find an answer (I also suspect all i really need is the right search term for Google). Please keep in mind this is a learning project (Although it is in use in a busin...

Why does LINQ-to-SQL Paging fail inside a function?

Here I have an arbitrary IEnumerable<T>. And I'd like to page it using a generic helper function instead of writing Skip/Take pairs every time. Here is my function: IEnumerable<T> GetPagedResults<T>(IEnumerable<T> query, int pageIndex, int pageSize) { return query.Skip((pageIndex - 1) * pageSize).Take(pageSize); } And my code is: ...

retrieve newly inserted record ID with LINQ To SQL

Using the following: private BlogDataContext db = new BlogDataContext (); article.Created = DateTime.UtcNow; article.Modified = DateTime.UtcNow; db.Articles.InsertOnSubmit(article); db.SubmitChanges(); int id = article.Id; I am wondering is this safe? Will it give me the Id of the article that the user just inserted, or will there ...

Simple way to return anonymous types (to make MVC using LINQ possible)

I'd like to implement MVC while using LINQ (specifically, LINQ-to-entities). The way I would do this is have the Controller generate (or call something which generates) the result-set using LINQ, then return that to the View to display the data. The problem is, if I do: return (from o in myTable select o); All the columns are read f...

LINQ to SQL Translation

Hi, Depending on how I map my linq queries to my domain objects, I get the following error The member 'member' has no supported translation to SQL. This code causes the error: public IQueryable<ShippingMethod> ShippingMethods { get { return from sm in _db.ShippingMethods select new ShippingMethod( ...

MVC-like Autogenerated Pages for Webforms

I noticed that MVC lets you pass in LINQ to SQL objects to its views and it will autogenerate Create, Update and View Pages based on the LINQtoSQL object. Is there anything for webforms that lets you do this kind of thing? (In would be nice if it had validators...) ...

Confused about Linq to SQL and repositories

For the last 4 years I have been working as an ASP.NET developer in a small company where all applications are built using the Smart UI anti-pattern in .NET 2.0. This means I have no experience with .NET 3.5 and things like LINQ and general concepts like repository and service layers. I realize that in order to find a new job, I need to ...

Getting mapping error. After dragging table with xml fields into dbml file and then compiling.

"Error 1 DBML1005: Mapping between DbType 'Xml' and Type 'System.Xml.Linq.XElement' in Column 'XML_LAYOUT' of Type 'QUEST_BLOCK' is not supported." The above is the error am getting. What am doing is dragging a table with xml fields as columns from server explorer into a dbml file. After that when i compile i am getting the above error....

Linq-to-SQL: How to perform a count on a sub-select

I'm still trying to get my head round how to use LINQ-to-SQL correctly, rather than just writing my own sprocs. In the code belong a userId is passed into the method, then LINQ uses this to get all rows from the GroupTable tables matching the userId. The primary key of the GroupUser table is GroupUserId, which is a foreign key in the Gr...

How to get block of unused ids in table with LTS?

I have dropped on this question: SQL query to find Missing sequence numbers. I want to achieve the same thing but with LINQ to SQL. To recap: I have a table with ids like 1,2,4,5,10,11,12,15, [...]. I want the unused ids 3,6,7,8,9,13,14, [...]. Can we create a temporary sequence and use the same logic as in T-SQL? I tried to create a...