linq-to-sql

Linq to Sql not submitting changes to database file in console app

I have an issue with my Linq to SQL Data Context no submitting in my console application. The following code is all that there is in the main() method. BlogRollerDataContext bdc = new BlogRollerDataContext(); bdc.Urls.InsertOnSubmit(new Url() { Approved = false, UrlTo = "http://www.google.com/", UrlFrom =...

Linq-to-sql: How to actively grab linked objects?

So I have a tree structure in a SQL-Server database. Each node is linked to its parent by a foreign key called prev, to another node. I would like whenever I fetch a node, for all nodes leading to the root of the tree to be fetched as well. Currently I can do it like this: MyDataContext db = new MyDataContext(); IList<N...

LINQ to SQL "1 of 2 Updates failed" on "SubmitChanges()"

I am updating an object of type X and its children Y using LINQ to SQL and then submitting changes and getting this error Example Code X objX = _context.X.ToList().Where(x => x.DeletedOn == null).First(); objX.DeletedOn = DateTime.Now; EntitySet<Y> objYs = objX.Ys; Y objY = objYs[0]; objY.DeletedOn = DateTime.Now; _context.SubmitChan...

Linq to SQL order by aggregate

I have a very simple grouping and aggregation problem in LINQ to SQL that I just can't figure out, and it is driving me mad. I've simplified things into this example: class Customer { public Guid Id; public String Name; } class Order { public Guid Customer_Id; public double Amount; } How do I get a list of customers order...

LINQ To SQL in Compact Framework

Im on to design my Data Access for a new solution i create. That solution though contains Compact Framework Device Application and libraries besides Desktop. All .NET 3.5. Desktop will handle all Data Access basically. I need the Data Objects to have in CF too, Desktop will communicate with SQL and then with Mobile and give the appropria...

Complex relationships with multiple LINQ

After a long thought we found that the best approach for our needs would be to use multiple tables as if it was classes, so we have something like: tblPerson (pk code) tblWorker (pk codePerson) tblPhone (pk code, fk codePerson) tblAddress (pk code, fk codePerson) it's almost like classes. It was really necessary because many tables do...

Can't get DataGridView to refresh over Linq to SQL (WinForm)

Very strange situation here: I'm using L2S to populate a DataGridView. Code follows: private void RefreshUserGrid() { var UserQuery = from userRecord in this.DataContext.tblUsers orderby userRecord.DisplayName select userRecord; UsersGridView.DataSource = UserQuery; //I have als...

SQL 'Execute As' Login Command and Linq to SQL

I am trying to execute a sql query as another login using the 'Execute As' command. I am using Linq to SQL, so I've generated a Data Context class and I am using the ExecuteQuery method to run the 'Execute As' SQL command. I then call a Linq to SQL command that is successful. However, every subsequent query fails with the following err...

Converting to an int in LINQ not working?

The following query complains that the int conversion is not supported. var list = from d in data where d.Id == (int)GridView1.DataKeys[0].Value select d; It complains on the (int)GridView1.SelectedInex line telling me that the Int conversion is not supported. I also tried Convert.ToInt32, but that did not work ...

Why did the following linq to sql query generate a subquery?

I did the following query: var list = from book in books where book.price > 50 select book; list = list.Take(50); I would expect the above to generate something like: SELECT top 50 id, title, price, author FROM Books WHERE price > 50 but it generates: SELECT [Limit1].[C1] as [C1] [Limit1].[id] as [Id], [Limit...

Best Linq2Sql equivalent of IsNull(Max(id))

I'm looking to convert the following SQL into Linq2SQL. select isnull(max(Id),0) from tbl Even though I have Id defined as Int NOT NULL I wish to be able to have a defult value of 0, even when there are no rows in the table. The best readable approach I've been able to come up with is var maxId = dc.tbl.Select(row => row.Id) ...

Linq to SQL - Count number of days in intersection between date ranges

Hey, I have a table that has multiple records with a start date (PlacementDate) and end date (Pulled Date). I'm passing start date and end date parameters into this function which needs to return the records that intersect the start and end date passed in and also specify how many days each record intersects for. Getting the records th...

How to use UDF output as LINQ to SQL entity class property value?

Just starting out with LINQ to SQL (you can probably tell - be gentle). I'd like to use the value returned by a user-defined function as an entity class property value and have the the value populated when all the other basic (column) fields are loaded. I know there are ways to use the UDF from a L2S query (here, and here), but I'd pre...

Linq to SQL in ASP.Net MVC Runs as Network Service when using Impersonation

I have an ASP.Net MVC controller action that instantiates a DataContext object and I am currently passing the connection string directly into the constructor. I am using Impersonation and I have verified the current user in the controller action is the current Windows Auth. user of the web app, however when running a SQL Trace the query...

Retrieving data from a one-to-many relationship

I'm trying to retrieve data from a one-to-many relationship, in an ASP.NET MVC application. Let's say I have two tables Posts and Category with the following attributes: posts -id -title -category_id category -id -name -author I want to grab posts that belong to a category and grab the values of .name and .author from the category. T...

mocking LINQ to SQL

What is the best and easiest way to unit test a Class that uses LINQ to SQL and returns back a decimal, is it by Mocking? If so how do I go about this? ...

Extension method on datacontext Linq to sql

Is it possible to create an extension method on the DataContext, not on the table in the datacontext but directly on the dataContext to get dynamicly a table. ex: DataContext dc = new DataContext(); var test = from a in dc.myExtensionMethod(args) select a; ps: I Already know dc.GetTAble and dc.GetTable<T> ...

LINQ 2 SQL N-Tier Application

Hello all, im trying to test and then implement LINQ 2 SQL as my Data Access to my N-Tier Application, as i read some info today, i added to my database a DateTime column as this reflected in my L2S Designer i changed the TimeStamp property of each "column" in the designer to true and this forced AutoGenerated Value to true as well. But ...

asp.net MVC Database call in partial view

I would like to make a database call in a partial view in asp.net MVC. I am not sure how to actually go about that. I am trying to get an instance of the repository so I can make a couple calls to the DB to build some info on the page. Not sure if I am even close but does anyone have any ideas ? <%@ Control Language="C#" Inherits="Sys...

Linq updating two tables I need Autoid value

I am doing some activity that will update two tables. One of the tables have an autoid which I need inserted in the other table. The field that stores the autoid value is used a like a foreign key to different tables depending on context. When updating I face the problem that the autoinc Id does not have a value until its saved. i hav...