linq-to-sql

How to change LINQ O/R-M table name/source during runtime?

I've got a database, and an entityset created by the O/R-Mapper, using all this with LINQ. In the O/R-Mapper I need to enter a table name (source) for every table, which is being used for the SQL generated by LINQ. In the .dbml file it looks like this: <Table Name="dbo.Customers" Member="Customers"> Now I'd like to change this table ...

In a WebService with linq-To-sql can the datacontext be a static field?

I'm creating a webserivce that will be using Linq-To-Sql to perform select only queries. In this respect would having the DataContext as a static field / property by acceptable since the operations will never be ones that modify the database or track object changes? If not, what alternative approaches would be suitable? ...

Salting and Hashing Passwords using Linq To SQL

I need to salt and hash some passwords so that I can store them safely in a database. Do you have any advice or ideas as to how best to do this using Linq To SQL? ...

Generating your LINQ-to-SQL datalayer

I usually use Codesmith & NetTiers for my datalayers, and as they can be run from the command line it just requires a simple batch file to regenerate / rebuild everything whenever changes are made to the database. Now I'm looking at LINQ-to-SQL, but I'm not finding the WYSIWYG entity designer as convienient. What other methods are avail...

How to accomplish group join and multi table parameters in LINQ

I must have missed something in my LINQ training. In Linq to SQL using c#, I want to query two tables, one with the foreign key to the other, and pass two parameters, one for each table. IN SQL, it is something simple like Select Value from Table1 T1 INNER JOIN Table2 T2 On T1.DefID = T2.ID Where T1.PollID = 1 AND T2.Name = 'Quest...

How do you perform a left outer join using linq extension methods

Assuming I have a left outer join as such: from f in Foo join b in Bar on f.Foo_Id equals b.Foo_Id into g from result in g.DefaultIfEmpty() select new { Foo = f, Bar = result } How would I express the same task using extension methods? E.g. Foo.GroupJoin(Bar, f => f.Foo_Id, b => b.Foo_Id, (f,b) => ???) .Select(???) ...

Common Table Expression (CTE) in linq-to-sql?

Is it possible to do common table expressions (CTE) (like shown below) in Linq to SQL. I'm pretty new to CTE's as well as Linq to SQL. I'm currently Stored Proc free (but not against them by any means) so i don't want to take the leap to stored procs just for one query unless it's totally necessary. Here's an example of what I'm doing...

CTE to traverse back up a hierarchy?

I can find all the children of a given record in a hierarchical data model (see code below) but I'm not sure how to traverse back up the Parent/Child chain with a given Child ID. Can anyone point me in the right direction to figure out how to do this? Is this possible in Linq to SQL as well? WITH TaskHierarchy (TaskID, [Subject], Pare...

Trying to store password to database...

Hi i'm doing a test how hash and salt passwords. Well , i can add hash and salt password to the Database but i got stuck to store passwords from database. i have a simple Database : Table _______ ProvaHS -------...

How to create an XmlMappingSource during runtime?

(Follow-Up-Question to How to change LINQ O/R-M table name/source during runtime?) I need to change the table source of a LINQ 2 SQL O/R-Mapper table during runtime. To achieve this, I need to create an XmlMappingSource. On command line, I could use SqlMetal to create this mapping file, but I would like to create the mapping file during...

Is this an example of LINQ-to-SQL?

I made a little WPF application with a SQL CE database. I built the following code with LINQ to get data out of the database, which was surprisingly easy. So I thought "this must be LINQ-to-SQL". Then I did "add item" and added a "LINQ-to-SQL classes" .dbml file, dragged my table onto the Object Relational Designer but it said, "The se...

Compare nullable types in Linq to Sql

I have a Category entity which has a Nullable ParentId field. When the method below is executing and the categoryId is null, the result seems null however there are categories which has null ParentId value. What is the problem in here, what am I missing? public IEnumerable<ICategory> GetSubCategories(long? categoryId) { var subCate...

Why isn't my SubmitChanges() working in LINQ-to-SQL?

I created a .MDF database in my WPF application. I then generated LINQ-to-SQL classes and used LINQ get all the customers. Then I run through them and change each of their last names. However, when I call SubmitChanges, the database remains unchanged. I thought that was the purpose of SubmitChanges(), to submit changes to the databas...

How do i replace a linq object from a method call

I have a linq object that I'd like to "retire" when certain aspects are changed and the Save() method is called. The database does this by having a Terminated column which when set causes the object to be ignored on subsequent queries. I'd like to do this transparently so for example: DataContext db = new DataContext(); Foo bar = (from ...

Help! - SQL - IF Else IF logic for returning Containstable selection

Hi guys, I'm trying to use IF else If logic inside an inline table valued function for SQL and returning a containstable based on that logic. but i'm having syntax problems with the IF Else IF block. thanks for the help. since i can't parametrize the columns in the containstable i have to resort to using if else statements. here's the co...

LINQ - Add property to results

Is there a way to add a property to the objects of a Linq query result other than the following? var query = from x in db.Courses select new { x.OldProperty1, x.OldProperty2, x.OldProperty3, NewProperty = true ...

LINQ to SQL exception: System.OutOfMemoryException

Not sure why I keep getting an OutOfMemory exception. I'm using ASP.NET MVC with LINQ to SQL. Here's some of the stack trace: [OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.] System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IntPtr method) +0 System.Reflection.Emit.Dynamic...

Linq to SQL - LoadWith used to load multiple child tables.

Here's what I'm doing: opts.LoadWith<Job>(j => j.Questions); opts.LoadWith<Job>(j => j.Categories); I keep getting an error that "Specified cast is not valid." However, if I do either of the alone, it works! ...

TreeView, Linq-To-SQL recursive data population

I have an IEnumerable(of Employee) with a ParentID/ChildID relationship with itself that I can databind to a TreeView and it populates the hierarchy perfectly. However, I want to be able to manually loop through all the records and create all the nodes programmatically so that I can change the attributes for each node based on the data ...

How to move from Linq 2 SQL to Linq 2 Entities?

I'd like to start a reference for people who want to move from linq2sql to linq2entities and the ADO.net Entity Framework (in here called L2E). I don't want to discuss which of these two is better. I just want to create a list of differences between these two for people who want to transition from one to the other. The basic stuff is ea...