linq-to-sql

LINQ to SQL -Internals -C#

Does LINQ to SQL work in connected environment or disconnected environment? I mean if you compile the query it builds expression query and the query is sent down to sql server ,there it is translated into T-SQL statement and executed and the final result is sent back to the C# code.I hope it is working in connected environment.Is there a...

Build and render infinite hierarchical category tree from self-referential category table

I have a Categories table in which each category has a ParentId that can refer to any other category's CategoryId that I want to display as multi-level HTML list, like so: <ul class="tree"> <li>Parent Category <ul> <li>1st Child Category <!-- more sub-categories --> </li> <...

Linq2SQL to produce Like operator

Hi, I have a string "Word1 Word2" and I want to transform it to a query such as "Like '%Word1%Word2%'". At the moment I have: from t in Test where t.Field.Contains("Word1 Word2") How to do this in LINQ2SQL? Do I need to create two separate queries for this, can't I write it in the same statement? Thx in advance ...

ASP.NET MVC Stored Procedure

I am using a ms sql stored procedure to get a set of records , when i am adding this stored procedure to my LinqToSql class and using it my repository it shows like it is returning a int value but it should be returning a set of rows. Is there some thing wrong with my stored procedure or some thing else??? ALTER PROCEDURE [dbo].[GetDoc...

LINQ2SQL DataLayer / Repository Suggestion

My current respository is as follows , please suggest , i am currently using LINQ2SQL Data context per insert/delele/update namespace Lib.Repository { public class MotorRenewalDataRepository { public MotorRenewalDataRepository() { } public MotorRenewalData GetByID(long id) { ...

Self-referencing tables in Linq2Sql

Hi, I've seen a lot of questions on self-referencing tables in Linq2Sql and how to eagerly load all child records for a particular root object. I've implemented a temporary solution by accessing all underlying properties, but you can see that this doesn't do the performance any good. The thing is though, that all records are correlated...

LINQ InsertOnSubmit Required Fields needed for debugging

Hi All, I've been using the ADO.NET Strogly-Typed DataSet model for about 2 years now for handling CRUD and stored procedure executions. This past year I built my first MVC app and I really enjoyed the ease and flexibility of LINQ. Perhaps the biggest selling point for me was that with LINQ I didn't have to create "Insert" stored proced...

Using LINQ to SQL Join to add one column to a complex object

I have a table with more than 100 columns. I need to join it to another table to pick up one more column. Do I have to list every column in my left table or is there a simpler way. var query = from p in context.policies join s in context.states on p.state_id equals s.state_id select new { ...

LINQ 2 SQL: Partial Classes

I need to set the ConnectionString for my DataContext's based on an AppSetting. I am trying to do this by creating a Partial Class for each DataContext. The below is what I have so far and I am wondering if I am overlooking something? Specifically, am I dealing with my DataContext's correctly(disposing, staleness, etc)? Doing it th...

SQL Group By and Join

I feel totally stupid. I'm rusty with my sql. I have two tables, Message and MessageThread. Each message belongs to one MessageThread using ParentTHreadID as a Foreign Key. You probably can see where this is going. Well, I want to do something like this. I want to get columns from both tables, messages and threads, but where the mes...

Linq to SQL NullReferenceException's: A random needle in a haystack!

I'm getting NullReferenceExeceptions at seemly random times in my application and can't track down what could be causing the error. I'll do my best to describe the scenario and setup. Any and all suggestions greatly appreciated! C# .net 3.5 Forms Application, but I use the WebFormRouting library built by Phil Haack (http://haacked....

How do I use Linq-to-sql to iterate db records?

I asked on SO a few days ago what was the simplest quickest way to build a wrapper around a recently completed database. I took the advice and used sqlmetal to build linq classes around my database design. Now I am having two problems. One, I don't know LINQ. And, two, I have been shocked to realize how hard it is to learn. I have...

Strange exception while using linq2sql

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: mapping Source Error: Line 45: #endregion Li...

The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type

I'm getting the following error in my MVC2 app using Linq to SQL (I am new to both). I am connected to an actual SQL server not weird mdf: System.InvalidOperationException The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type My SQL table has a column called MessageID. It is BigInt typ...

querying databases on same server with linq

In normal sql I could do joins on tables in different databases as long as they were on the same server (or linked servers). In linq I can't figure out how to do that. Is this possible? For example, if I have a database called db1 and another called db2. db1 has a table called people and db2 has a table called address I could do some...

How to Expression.Invoke an arbitrary LINQ 2 SQL Query

Say I take an arbitrary LINQ2SQL query's Expression, is it possible to invoke it somehow? MyContext ctx1 = new MyContext("..."); var q = from t in ctx1.table1 where t.id = 1 select t; Expression qe = q.Expression; var res = Expression.Invoke(qe); This throws ArgumentException "Expression of type System.Linq.IQueryable`1[...]' cannot ...

LINQ Datacontext Disposal Issues

I am getting a Cannot access object: DataContext after it's been disposed in the below DAL method. I thought that I would be okay calling dispose there. result is an IEnumurable and I thought it was IQueryable that caused these kinds of problems. What am I doing wrong? How SHOULD I be disposing of my DataContext. Is there somethin...

LInq to sql query

Hi, I have a sql query as follows: Declare @DivisionNo INT SET @DivisionNo = 5117 SELECT distinct CASE WHEN ISNULL([DivisionNo],'') <> @DivisionNo THEN @DivisionNo ELSE [DivisionNo] END as DivisionNo --,[RecordID] ,[AcctCat] ,[AcctCatDesc] ,[CostCode] ,[Co...

help converting sql to linq expression with count

I am trying to convert the following SQL into a LINQ expression SELECT COUNT(ID) AS Count, MyCode FROM dbo.Archive WHERE DateSent>=@DateStartMonth AND DateSent<=@DateEndMonth GROUP BY MyCode and I have been trying to follow this webpage as an example: http://stackoverflow.com/questions/606124/converting-sql-containing-top...

How do I simulate "In" using Linq2Sql

I often find myself with a list of disconnected Linq2Sql objects or keys that I need to re-select from a Linq2Sql data-context to update or delete in the database. If this were SQL, I would use IN in the SQL WHERE clause, but I am stuck with what to do in Linq2Sql. Here is a sample of what I would like to write: public void MarkValidate...