linq-to-sql

Linq query with nullable sum problem

from i in Db.Items select new VotedItem { ItemId = i.ItemId, Points = (from v in Db.Votes where b.ItemId == v.ItemId select v.Points).Sum() } I got this query, however it fails if no vots are found with "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable valu...

Linq To Sql Need Dynamic Where Clause over relational tables Help?

I need Help for dynamic where clause over relational tables (one to many) in LinqToSql. User select conditions from page. (there is 4 input that user select the clauses) For example CompanyName and CompanyTitle from Customer table and OrderDate and ShipCity From Order table. But user can select one ore many of them from page interfa...

Parsing IIS configuration xml doc with linq to xml

Hi Guys, I am writing a small app that finds all the folders IIS is referencing. To do this I take the IIS config file and parse the xml looking for elements called IIsWebVirtualDir and look for the Path attribute. Here is my code XDocument doc = XDocument.Load(xmlPath); IEnumerable<XElement> elements = doc.Elements(...

LINQ for LIKE queries of array elements

Let's say I have an array, and I want to do a LINQ query against a varchar that returns any records that have an element of the array anywhere in the varchar. Something like this would be sweet. string[] industries = { "airline", "railroad" } var query = from c in contacts where c.industry.LikeAnyElement(industries) select c Any id...

How to map many tables with same schema to one class

Is there a way in entity framework or linq to sql classes to map one class to many different tables with the same schema. For example if I have a database with thousands of tables for different stocks. All of the tables have the same columns. Is there a way I could have a base type class that could be used for mapping the data to an o...

LINQ: Select data from a single column in IQueryable<>

Hi, I have a table that I need to extract data from, and wish to discard one of the two columns that the data comes from. In my DB, I have "ObjectID (PK)" and "ObjectName". I wish to use this data to populate a SelectList in an ASP.NET MVC project, and so have an IQueryable object in my code which looks as follows: public IQueryable<o...

LINQ to SQL Left Outer Join

Hi, Is this query equivalent to a LEFT OUTER join? //assuming that I have a parameter named 'invoiceId' of type int from c in SupportCases let invoice = c.Invoices.FirstOrDefault(i=> i.Id == invoiceId) where (invoiceId == 0 || invoice != null) select new { Id = c.Id , InvoiceId = invoice == null ? 0 : invoice.Id } ...

Type 'Microsoft.SqlServer.Types.SqlHierarchyId' is not supported for identity members.

I'm getting the above exception while trying to read data from a table using Linq2Sql. Does anyone know what's happening here? ...

How do I speed up or remove loading from linqtosql

I have two tables, Transaction and TransactionLineItem that are related through a 1:n relationship (TransactionLineItem has a TransactionKey field that points to the Transaction). I've created the Transaction and exactly 1 TransactionLineItem and posted them to the database. Transaction tran = new Transaction(); context.Transactions.Ins...

Compiled LinQ queries

I'd like to see a basic and clear example of how to compile a LinQ to SQL query. I've googled about it, and even though there are a couple of implementation examples, usually blog posters emphasize on the time response difference between compiled and non-compiled queries. ...

Another Linq2Sql Lazy loading question

Hi I have a read-only database, so I am turning off ObjectTracking (thus implicitly turning off DeferredLoading). I wish to do lazy loading and not use LoadWith<>. What is the simplest way to explicitly tell Linq to go and lazy fetch a relation just before I need the data itself. For example: a simple dbml If I have the following co...

Changing the order of LINQ to SQL Inserts

I have two LINQ to SQL classes, CandyBar and DeliciousCandyBar that map to tables of the same name in SQL Server. There is a 0..1 relationship between CandyBar and DeliciousCandyBar. i.e, A CandyBar can have 0 or 1 DeliciousCandyBars. Conversely a DeliciousCandyBar has exactly one CandyBar. In LINQ to SQL class, they look (basically) l...

Linq To Sql - Update not being persisted.

I am really confused about why an update is not taking place. This is very simple: int goodID = 100; DataContext db = new DataContext(); Schedule schedule = db.Schedules.Single(s => s.ID == goodID); // this wont persist - WHY NOT?! schedule.Email = txtEmail.Text; // this does persist schedule.NumberCourse...

Ignore read-only class properties when using DataContext.ExecuteQuery<T>

How do I tell a LINQ data context to ignore either specific properties, or all readonly properties, when binding a result set to an object? I am working with some T-SQL statements that are difficult to express using LINQ, so I'm using the ExecuteQuery method of the data context to pass the straight T-SQL to the database. If my class T ...

DataContext in CodeBehind?

hey all, Im working on an ASP.NET app and using LINQ to SQL for the first time and something seems off. Usually on a web project, there is a data access layer, a biz layer and the preso layer....does linq to sql not work this way? I see where other developers have put the DataContext directly into the Preso layer and are calling the DB v...

Unicode Collations problem ?

(.NET 3.5 SP1, VS 2008, VB.NET, MSSQL Server 2008) I'm writing a small web app to test the Khmer Unicode and Lao Unicode. I have a table that store text in Khmer Unicode with the following structure : [t_id] [int] IDENTITY(1,1) NOT NULL [t_chid] [int] NOT NULL [t_vn] [int] NOT NULL [t_v] [nvarchar](max) NOT NULL I can use Linq to SQL...

Efficient Search function with Linq to SQL

Hi, I'm using VB.NET and Linq to SQL. I have a table with thousands of rows and growing. Right now I'm using .Contains() in the Where clause to perform the query. Below is my search function : Public Shared Function DemoSearchFunction(ByVal keyword As String) As DataTable Dim db As New BibleDataClassesDataContext() Dim query =...

LINQ to SQL eager loading with conditions

I'm trying to learn LINQ to SQL and i've found out about the LoadWith function. All the examples i've found will load all records from the table you specify in the LoadWith function e.g. var dlo = new DataLoadOptions(); dlo.LoadWith<Blog>(b => b.Posts); this.LoadOptions = dlo; What I would like to know is if it's possible to load in t...

Vexing linq to sql predicate building in a for loop

I'm building a LINQ query using a loop that appends predicates using an array: foreach (string tag in tags) { result = result.Where(p => (p.TagsDelimited).Contains("," + tag + ",")); } This creates all the necessary clauses, but each clause compares only the last element in the tags array, producing the sql (((',' + [t0].[TagsDel...

DataContext. Is there a single method where I can add an extra WHERE?

So basically I have an application that works with just one user, but I'd like to make it multi-user. This would involve me logging a user in, and keeping the user ID in session, plus adding a user id column to my database tables. no biggy. I'd like to change my dbml, or use it's partial, so that any query I throw through it also gets ...