linq-to-sql

Most succinct LINQ To SQL for taking COUNT(*) of either side of many-to-many?

Please help me with a sanity check. Assuming a many-to-many relationship: What's the most succinct way (using LINQ to SQL) to get a result set showing, for each tag (or post), the aggregate number of posts (or tags) assigned to it? Thanks! ...

Good sophisticated linq to sql sample?

I am looking for a more sophisticated way of using Linq to SQL. I am leaning L2Q but I am only seeing simple examples of business objects and simple CRUD operations in them. Any sample application that includes these: 1- Business objects which get their data from more than 1 table and from many to many tables. 2- Keeping track of dat...

How to add a SqlMetal build step in VS2008?

I've created a one-line batch file to run SqlMetal to regenerate a LINQ to SQL DataContext for my database; this works great. *Refresh_DataContext.bat*: "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sqlmetal.exe" /server:.\sqlexpress /database:MyDatabaseName /code:"%~dp0\DataContext.vb" /context:DataContext /vie...

Accessing members of the other half of a partial class

I'm just learning to work with partial classes in VB.NET and VS2008. Specifically, I'm trying to extend a LINQ to SQL class that was automatically created by SqlMetal. The automatically generated class looks like this: Partial Public Class DataContext Inherits System.Data.Linq.DataContext ... <Table(Name:="dbo.Concessions")> ...

LINQ to SQL -

I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFrom...

Linq to Sql - Loading Child Entities Without Using DataLoadOptions?

Is it possible to load child entities in a single query without using DataLoadOptions? I am using one data context per request in an asp.net web application and trying to get around the linq to sql limitation of not being able to change dataloadoptions once a query has been executed. Thanks. ...

Injecting dependency to Linq to Sql

Is there a mechanism to inject dependencies into Linq to Sql or entity framework entities? If so would it be a sensible approach? ...

How to deal gracefully with null foreign keys in Linq to SQL?

In my database, Concessions have a many-to-one relationship with Firms (each Concession has a FirmID). SqlMetal has captured this relationship and generated the appropriate classes, so that each Concession has a Firm element. I'm binding against a query (simplified here) that returns a list of Concessions along with information about the...

How to store xml files in an xml column using linq to sql?

I have xml files I want to read in and store in the database. Linq to sql requires the data to be sent in using an xelement, but if I send in the data this new XElement("build", BuildNode.InnerXml) I end up with data looking like this in the database lt;..gt;.. It converts the brackets into lt; and gt; which then makes the data useles...

Should I still see the query hit in SQL Profiler?

I am currently building a web site and I just implemented SqlCacheDependency using LinqToSQL like so. public IQueryable<VictoryList> GetVictoryList() { string cacheKey = HttpContext.Current.User.Identity.Name + "victoryCacheKey"; IQueryable<VictoryList> cachednews = (IQueryable<VictoryList>)HttpCont...

SqlCacheDependecy command notification not working

I been trying to get sqlcachedependecy working, but it doesn't appear to work I got the proper settings in my web.config and also global.asa, however when I run this query and the changes are made to the database from either with in or outside the web site the cached objects are not updated please someone help? I know its not because t...

LinqToSql Best Practices

I just started creating my data-access layer using LinqToSql. Everybody is talking about the cool syntax and I really like Linq in general. But when I saw how your classes get generated if you drag some tables on the LinqContext I was amazed: So much code which nobody would need?! So I looked how other people used LinqToSql, for examp...

Linq-to-SQL ToDictionary()

How do I properly convert two columns from SQL (2008) using Linq into a Dictionary (for caching)? I currently loop through the IQueryable b/c I can't get the ToDictionary method to work. Any ideas? This works: var query = from p in db.Table select p; Dictionary<string, string> dic = new Dictionary<string, string>(); forea...

What to do about @p LINQtoSQL parameters? What is @p anyhow?

Okay still fighting with doing some SqlCacheDependecy in my Asp.net MVC application I got this piece of code from Microsoft to cache LINQtoSQL, basically what it does is it gets the SqlCommand text from the LINQ query and executes that via the System.Data.SqlClient.SqlCommand which SqlDependecy needs... However there is one slight prob...

How does SQL know what @p# in LINQtoSQL queries?

I want to know how does the SQL Server know what @p# is in say this LINQtoSQL quey SELECT [t0].[MemberID], [t0].[Aspnetusername], [t0].[Aspnetpassword], [t0].[EmailAddr], [t0].[DateCreated], [t0].[Location], [t0].[DaimokuGoal], [t0].[PreviewImageID], [t0].[LastDaimoku] AS [LastDaimoku], [t0].[LastNotefied] AS [LastNotefied], [t0].[La...

SQL to Linq Expression?

Does anyone know of a tool to take a T-SQL query and convert it into a LINQtoSQL query? ...

Keeping a "reference" to a row object for a long time

In my application I keep a LINQ object fetched from the database, and regularly modifies it followed by db.SubmitChanges(). I have read that you are not supposed to keep around the DataContext object for a long time. But if I close my DataContext between modifications, I guess the data object loses its link to the actual database row. ...

LinqToSQL Not Updating the Database...

// goal: update Address record identified by "id", with new data in "colVal" string cstr = ConnectionApi.GetSqlConnectionString("SwDb"); // get connection str using (DataContext db = new DataContext(cstr)) { Address addr = (from a in db.GetTable<Address>() where a.Id == id select a).Single<Add...

LINQ2SQL Database providers

I have heard that LINQ2SQL has been written to support multiple database vendor providers however for internal political reasons at MS, only SQL Server has been supported. Is this a fair summary of the situation, and if so has a timetable been released as to when other databases will be supported? I am aware of Matt Warren's BUILDING A...

Best way to avoid SQL injection on MSSQL Server from C# code using Linq?

What is the best way to avoid SQL injection on MSSQL Server from C# code using Linq? Should you use a function to strip of security issues or is it handled by the framework? ...