linq-to-sql

Trying to get max date from table using Linq2Sql

The following linq2sql code is causing me much headache and I was hoping someone could help DateTime _maxRecordedDate = (from snapshot in _ctx.Usage_Snapshots where snapshot.server_id == server_id orderby snapshot.reported_on descending ...

Export DBML to SQL script

Due to a harddrive crash, I lost my SQL server database files for a project I was creating. I still have my DBML files I used in my .NET project, so I still have the database structure in a file. Is it possible to export my DBML somehow to a SQL server script, to recreate my databases in SQL server. ...

Are groups in Linq to Sql already sorted by Count() descending?

It appears so, but I can't find any definitive documentation on the subject. What I'm asking is if the result of this query: from x in Db.Items join y in Db.Sales on x.Id equals y.ItemId group x by x.Id into g orderby g.Count() descending select g.First() is ALWAYS THE SAME as the following query: from x in Db.Items join y in Db.Sal...

How to handle Null in LINQ Subquery?

I've got a subquery that returns the most recent value from a child table. In some cases the subquery returns nothing. The query below fails at runtime because the inferred type of MemberPrice is decimal and is not nullable. Simplified query: Dim q = From s In dc.STOCKs _ Select s.ID, MemberPrice = _ (From mp In dc...

Paging on a Random Items Data Set

I have to generate a list of items on a website which are random for the session of the user for that particular list of items. I am going to add a link to demonstrate the problem. WebSite Link Scenario: When a user comes in and clicks on the link, the items on the page should be randomized. As the user clicks on page two, three on...

Persistance Ignorance Linq to SQL

I have an existing domain layer. I want to develop the persistence layer using Linq to SQL. I am currently using an external map file. I am trying to use lazy loading for my child collections but am unsuccessful. Is there a way to implement lazy loading using Linq to SQL but without using EntitySet or EntityRef. ...

Linq to SQL Entity Update

I have a parent and an a child entity. I can successfully creates a parent and created a child linked to it's parent. So my problem comes if I try to update the child. I get an error about foreigh key constraints. What am I doing wrong? ...

Instead of trigger in SQL Server loses SCOPE_IDENTITY?

I have a table where I created an INSTEAD OF trigger to enforce some business rules. The issue is that when I insert data into this table, SCOPE_IDENTITY() returns a NULL value, rather than the actual inserted identity. Insert + Scope code INSERT INTO [dbo].[Payment]([DateFrom], [DateTo], [CustomerId], [AdminId]) VALUES ('2009-01-20',...

Update using linq

how can i update a record against specific id in (Linq to sql) ...

Speed test Linq2SQL v Entiry Framework

Has anyone compared the speed of performance between LINQ to SQL against the Entity Framework? I have heard that LINQ to SQL is 4 times faster than the Entity Framework, but I've never seen any bench marks or sample application proving this. ...

Linq to Sql - Use sum in the where clause

I'm trying to select orders that have either over or under 2000 products ordered in them, depending on other values. I need to select the information from the Orders table, but check this value in the OrdersProducts table, specifically the sum of OrdersProducts.ProductQty. I also need to do this using predicate builder, because of othe...

Scalar function in LinqToSql

In ADO.Net/SQLClient I would often do something like this: SELECT COUNT(*) FROM SomeTable WHERE SomeKey = 1234 ...and fire it using executescalar to return the value of count - for a simple check if something exists. How would I do the same using LinqToSql? ...

Are subqueries in LinqToSql guaranteed to be in the same order as their parent?

lets say i have a table with a DateTime field and a int field and perform a query like this: var query = context.tblSomeTable.Where(s=>s.Name == "Hello World").OrderBy(s=>s.SignUpDate); if I then perform two subqueries with the result, are they still ordered by the date?: var sub1 = query.Where(s=>s.Id = 5); var sub2 = query.Where(s=...

Linq and DB relationships

quick question here guys. I'm working with an older database, which had no relationships adn am now trying to make it as consistent as possible. the code that I'm porting had some quirks which led to some situations where I cannot enforce the relationship (PK <-> FK). I was wondering if this enforced relationship is a requirement for L...

Linq Distinct() by name for populate a dropdown list with name and value

Hi! I'm trying to populate a Drop down list with pharmaceutical companies, like Bayer, Medley etc. And, I'm getting theses names from DB and theses names are repeated in DB, but with different id's. I'm trying to use Linq Distinct(), but I don't want to use the equality comparer. Is there another way? My drop down list must be filled ...

How to access a child EntitySet in the OnLoaded() partial method

I've got a LINQ to SQL entity called Book that has a child collection of Authors that represents a relationship between tables of the same name in the db. What I want to do is that when the Book entity is "hydrated" with data from the db, I want to initialize an AuthorsManager object I've created to ease working with the Authors EntityS...

keep LinqToSQL sync with the database

Hello, I am going to develop a new website with asp.net 3.5 and LinqToSQL. For maintainability purposes, how can I modify a Linq class if an attribute is added to a table in the database ? Thank you. ...

Order a linq query

Hello Stackers. For my generic grid I currently do this to activate the sorting: Elements.OrderBy(column.SortExpression).AsQueryable(); In which SortExpression is of type Func<T, object> and column is a generic class Column<T> I set SortExpression in a controller like this: new Column<OrderViewData>{Key = "ShippingDate", SortExpres...

Linq2Sql: changing "delete" extensibility method to update a record

I am writing a CRUD using winforms, connecting to MS SqlServer 2008 via Linq2Sql. When the user of my application wants to delete a record in the database I dont't want it physically deleted. I just want to set a column called "DlDat" to the current datetime and then update the record instead of deleting it. To force this behaviour in Li...

LinqToSql - avoiding excessive queries to the database

This scenario comes up often, now that I use LinkToSql and the classes it creates. Classic scenario - two tables: Member ID Name ... Membership ID MemberID (foreign key) Start (datetime) Expiration (datetime) ... An active membership would be one where now is between start and expiration. To find out if a user has an active members...