linq

LINQ Issue: Unable to cast object of type 'System.Reflection.Module' to type 'System.Reflection.Emit.ModuleBuilder

I have a simple lambda expression which runs fine as a UNIT test and also runs fine when I copy the code into the Main method of my application. However, when I run the same piece of code within a callback method (via JMS courier) I get the above error. Has anyone encountered this? Example code failing: Expression<Func<JupiterDividend,...

LINQ to SQL associations - how to change the value of associated field

I have 2 classes with a LINQ association between them i.e.: Table1: Table2: ID ID Name Description ForiegnID The association here is between Table1.ID -> Table2.ForiegnID I need to be able to change the value of Table2.ForiegnID, however I can't and think it is because of the association (as wh...

Getting Count from Grouped DataTable in VB via Linq

I'm running into a mental roadblock here and I'm hoping that I'm missing something obvious. Anyway, assume I have a table that looks like this: ID LookupValue SortOrder ============================================ 1 A 1000 2 B 2000 3 B ...

LINQ equivalent of foreach for IEnumerable<T>

I'd like to do the equivalent of the following in LINQ, but I can't figure out how: IEnumerable<Item> items = GetItems(); items.ForEach(i => i.DoStuff()); What is the real syntax? ...

[LINQ] EntitySet vs Table

In a LINQ to SQL class, why are the properties that are created from the foreign keys EntitySet objects, which implement IEnumerable, where as the objects on the DataContext are Table objects which implement IQueryable? EDIT: To clarify, here is an example that illustrates what I'm trying to understand. This example: ctx.Matches.Where(...

How do I prevent inserting new records into tables while using Linq and SQL?

When creating a new object that is mapped to one of my SQL Server tables, LINQ inserts a new record into the table when I call SubmitChanges. I want to prevent this, but still need my new object. I know about the custom methods, but is there anyway to disable this behaviour so that it just updates existing records and never inserts new...

Persistence framework?

I'm trying to decide on the best strategy for accessing the database. I understand that this is a generic question and there's no a single good answer, but I will provide some guidelines on what I'm looking for. The last year we have been using our own persistence framework, that although limited has server as well. However it needs some...

What is more readable?

I have these two pieces of code, wich one is more readable? foreach decimal technicalPremium = 0; foreach (Risk risk in risks) { technicalPremium = technicalPremium + risk.TechnicalPremium; } return technicalPremium; linq return risks.Sum(risk => risk.TechnicalPremium); ...

How do I ensure Linq to Sql doesn't override or violate non-nullable DB default values?

I have an SQL Server DB with a table with these fields: A bit with the default value 1, NOT NULL. A smalldatetime with the default value gettime(), NOT NULL. An int with no default value, IDENTITY, NOT NULL. When I generate Linq to SQL for this table, the following happens: The bit is given no special treatment. The smalldatetime i...

Linq to SQL: .FirstOrDefault() not applicable to select new { ... }

I just asked this question. Which lead me to a new question :) Up until this point, I have used the following pattern of selecting stuff with Linq to SQL, with the purpose of being able to handle 0 "rows" returned by the query: var person = (from p in [DataContextObject].Persons where p.PersonsID == 1 select...

Hierarchical data in Linq - options and performance

I have some hierarchical data - each entry has an id and a (nullable) parent entry id. I want to retrieve all entries in the tree under a given entry. This is in a SQL Server 2005 database. I am querying it with LINQ to SQL in C# 3.5. LINQ to SQL does not support Common Table Expressions directly. My choices are to assemble the data in ...

Preserving order with LINQ

Hi. I use linq To Objects instructions on an ordered array. Which operations shouldn't I do to be sure the order of the array is not changed? ...

LINQ to SQL error message: 'Where' not found

I'm trying to start using LINQ and specifically LINQ to SQL but I'm having some difficulties I've tried this with SqlMetal and now using the database table designer in Visual Studio and I keep getting similar errors, like in this code, using the data context I created with the database layout designer in VS2008. using System; using Sys...

Best way for retrieving single record results in LINQ to SQL

If I query a table with a condition on the key field as in: var user = from u in dc.Users where u.UserName == usn select u; I know that I will either get zero results or one result. Should I still go ahead and retrieve the results using a for-each or is there another preferred way to handl...

Is there a linq-y way to union collection properties of a collection of objects?

Sorry, that title just hurts. I'm wondering if there is a Linq to collections extension method that collapses the following code segment into a single line: public IEnumerable<Child> GetAllChildren(IEnumerable<Parent> parents){ var result = new List<Child>(); foreach(Parent parent in parents) foreach(Child child in parent.Chi...

LINQ External Mapping to Class Library

I've been struggling with a problem for the past couple days and haven't found a solution. I have an Visual Studio solution with 2 projects, the first one is a DLL with my business objects and logic, the other project is my WinForm application, and a reference dependency on the first project. I initially wrote the business objects with...

LINQ not updating on .SubmitChanges()

Is there any reason something like this would not work? This is the logic I have used many times to update a record in a table with LINQ DataClasses1DataContext db = new DataClasses1DataContext(); User updateUser = db.Users.Single(e => e.user == user); updateUser.InUse = !updateUser.InUse; db.Log = new System.IO.StreamWriter(@"c:\t...

ExpressionType.Quote

What is the purpose of this UnaryExpression, and how should it be used? ...

Object Oriented Model on top of LINQ to SQL

I'm playing a bit with LINQ to SQL and overall it's a much better option than what Microsoft had before (DataSet), but it seems that object-oriented capabilities are still limited. Since we currently use a custom persistence framework that created a OO model on top of DataSet, I'm looking to port the framework to a new version building a...

IndexOutOfRangeException on Queryable.Single

I have an ASP.NET site that has been running perfectly for a long time, nothing's changed recently. From one hour to the next I started receiving an IndexOutOfRangeException in a line where I do a LINQ query like this: var form = SqlDB.GetTable<ORMB.Form, CDB>() .Where(f => f.FormID == formID) .Single(); ORMB.Form is a POCO ob...