linq-to-sql

Using a DataContext static variable

I have recently inheited a ASP.Net app using Linq2SQL. Currently It has its DataContext objects declared as static in every page, and i create them the first time i find they are null (singleton, sort of). I need comments if this is good or bad. In situations when I only need to read from the DB and in situations where i need to write a...

Generic way to find or create an object in Linq to Sql?

Quite often in my Linq to Sql code, I need to "find or create" an entity as such: var invoiceDb = ctx.Invoices.FirstOrDefault(a => a.InvoicerId == InvoicerId && a.Number == invoiceNumber); if (invoiceDb == null) { invoiceDb = new Invoice(); invoiceDb.Number = invoiceNumber; ct...

Nested listview databinding - property not found?

I have a listview, where I set the datasource in the code-behind - this works fine. When I add another listview (or databound control) to the listview's itemtemplate, and set the datasource for that control in the codebehind, the fields returned by the query seem to be unavailable to the nested listview; ASP.NET throws the following erro...

Explicit construction of entity type '###' in query is not allowed.

Hi there, Im trying to instance a Entity called "Produccion" from LINQDataContext in this way : Demo.View.Data.PRODUCCION pocoProduccion = (from m in db.MEDICOXPROMOTORs join a in db.ATENCIONs on m.cmp equals a.cmp join e in db.EXAMENXATENCIONs o...

Linq to SQL - Return top n rows

I want to return the TOP 100 records using Linq. ...

Converting a LinqToSql DAL to EntityFramework

I've got an app that is just over 1yr old but it still evolving and will likely revolve more and more around a growing database schema in the near to mid term. The app was written using LinqToSql which has met our needs fairly well. We have run into a few pain points but have worked around them. I would currently consider this app to hav...

Default column values in Linq2Sql

Let's assume I have a table with a CreatedBy Datetime column with a default value of GETDATE()... Enter LinqToSql From what I understand so far, I have two options: Insert DateTime.Now manually in the linkToSql "insert" statement Set the "Auto Generated Value" to True, and now Linq2Sql will ignore a value I supply for CreatedDate ...

How can i get primary key value when i insert a new record ?

I am using LINQ-to-SQL class. It has a method object.InsertOnSubmit() .But it returns void so can i get primary key values after inserting a record. I want the primary key value of recently inserted record. ...

Getting distinct rows from a left outer join

I am building an application which dynamically generates sql to search for rows of a particular Table (this is the main domain class, like an Employee). There are three tables Table1, Table2 and Table1Table2Map. Table1 has a many to many relationship with Table2, and is mapped through Table1Table2Map table. But since Table1 is my main t...

Question about ComboBox and Linq to Sql for Winforms...

Edit: More than a day. Still no answer! Is question not clear? I have ComboBox bound to BindingSource which in turn is bound to Linq Table. Also, ComboBox is filled from datasource which is a Linq Table. I want to assign new value to one of properties of BindingSource.Current whenever user selects item in ComboBox. I understood from M...

How do I match two identical database tables with LINQ?

I want to match 2 identical tables: sourceProducts (productName, ProductionDate, ManID, shipper, distributer) CommProducts (productName, ProductionDate, ManID, shipper, distributer) but the number of rows and the record contents may differ. How do I select a certain record = raw from one table and get its clone record from the other...

Retrieve multiple results set

I have a Problem after I restive data from multiple result set I update the data and save it and its updated in Sql but when I retrieve it again its bring the old data what I can do? ...

Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible'.

Can somebody please help me to find out the solution with this problem? I'm trying to insert the data into my database which has 2 tables Products (ProductID): 1 (IDNumber) : 200900110 (ProductName) : Pepsi Order (OrderID): 1 (Auto Increment by 1) (ProductID):1 (Date): 1/1/2009 The code is this: var db = new ProductOrder(); var id...

Filter Gridview results using Linq to Sql Object datasource with a LIKE operator

I have a gridview bound to a LINQ to Sql datasource. I would like to filter the the results in the gridview using the LIKE operator. i.e I have a textbox used to Search on Username and I would like to select all users with the username like [textbox value]. Below is my code: <h1>Manage Users</h1> Search for users Username: ...

Combine two counting queries into one Boolean query

I can't think of a good way to write this as a single query. int count1 = query1.Count(); int count2 = query2.Count(); return count1 > count2; Please note that I'm interested in ways to write a single query that returns a Boolean value and gets evaluated once on the server. Please note that I'm interested in ways to write this with ...

Paginated search results with LINQ to SQL

What's the best pattern to get paginated results with LINQ to SQL? I have the following scenario: Suppose I want to search items table by description. I can easily do: public IQueryable<Item> FindItemsByDescription(string description) { return from item in _dc.Items where item.Description.Contains(description); } Now, w...

How to get html content and convert it to PDF with itextsharp?

Does anyone know how to get a html content, convert it to pdf and save it to database? I've tried so many ways, but nothing seems to work. In some articles, it's written to use HTMLParse, in others HTMLWorker... sometimes throws an error "document has no pages"... sometimes, it just throws an exception but it didn't specify the error......

LINQ To SQL Grouping

Hi, Can some kind soul please lend me the Linq To SQL query for the following T-SQL Query SELECT e.EmployeeName, MIN(a.SignInTime), MAX(a.SignOutTime) FROM Employee e LEFT OUTER JOIN Attendance a ON e.Id = a.EmployeeId AND CAST (CONVERT (varchar, a.SignInTime, 106) AS DATETIME) = '28 APR 2009' GROUP BY e.EmployeeName The database sch...

Linq problem with inserting new rows that have references to existing records

(I believe this is the same problem as this one, but there's no answer there, and I think I can express the problem better here...) I have two Linq-to-SQL classes, State and County, where County has a FK to State. Here's some test code: State s = State.GetState("NY"); // here I do a load of a State class via the Linq DataContext Count...

Aggregate data with LINQ and ASP.NET MVC

Suppose i have a structure Widget id name and a structure called WidgetVoteCount widgetId (FK to Widget.ID) numberOfVotes I want to display instances of Widget along with a corresponding aggregate count on my View WIDGET-TITLE WIDGET-COUNT(which is the sum of WidgetVoteCount.numberOfVotes) Blue Widget 5 Purple Widge...