linq

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...

Linq and a select with two tables

Hi! I want to make this select using linq: Select cd.name from Content c, ContentDetail cd where c.id_contentTypeID = contentTypeId and c.id_contentID = contentID and cd.id_contentID = c.contentID; I have done the following but I don't know how to end the query: var list = from c in guideContext.Content, dc ...

Using reflection to address a Linqed property

I'm trying to writing a generic method that will load a record of a specific type, with a specific ID. Here's one way that works: public abstract class LinqedTable<T> where T : LinqableTable { public static T Get(long ID) { DataContext context = LinqUtils.GetDataContext<T>(); var q = from obj in context.GetTable<T>() whe...

Relational databases and OO languages

In a typical ASP.NET web application architecture, we are using a OO language such as C# and a relational database such as SQL server for data. I was reading a book on Linq that said "The problem is there is a gap between a OO programming language and a relational database" What exactly is the author trying to imply? EDIT: Thanks for...

Trying to parse XML tree with Linq to XML (C#)

Hi, I would like to reflect the XML tree in my object structure, but I am quite a beginner in LINQ to XML I have an XML with following structure: <questions> <question id="q1"> <number>1</number> <text>some text11</text> <answers> <answer> <v>some text11</v> </answer> <answer> <v>some text11</v> </a...

How to get linq to produce exactly the sql I want?

It is second nature for me to whip up some elaborate SQL set processing code to solve various domain model questions. However, the trend is not to touch SQL anymore. Is there some pattern reference or conversion tool out there that helps convert the various SQL patterns to Linq syntax? I would look-up ways to code things like the foll...

How to find duplicates in a List<T> quickly, and update the original collection

Let me start by saying I've read these questions: 1 & 2, and I understand that I can write the code to find duplicates in my List, but my problem is I want to update the original list not just query and print the duplicates. I know I can't update the collection the query returns as it's not a view, it's an anonymous type IEnumerable<T>....

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...

Force an IQueryable to execute?

I have a method that 'has no translation to SQL' that I want to perform on an IQueryable, is there a way to force the IQueryable to execute without having to store it in some intermediate class? ...

How do you guard for Null Reference exceptions in Linq To Xml?

<?xml version="1.0" encoding="utf-8" ?> <pages> <page id="56"> <img id="teaser" src="img/teaser_company.png"></img> </page> </pages> I have an xml file that defines additional resources for pages within a cms. What's the best way to guard for Null Reference exceptions when querying this file with LinqToXml? var page = (from...

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 ...

How do you retrieve records and all child records in one database hit in ADO.NET Entities?

Hi there, I would like to make one database call to retrieve the following data, but I'm struggling to figure out what the LINQ should look like. Here's my current implementation (I know it's bad!!): var photos = from photo in entitiesContext.Photo join pg in entitiesContext.PhotoGallery on photo.PhotoGallery.PhotoGalleryI...

My linq-query doesn't work when adding the xmlns-attribute to the root-element in my XML-document

Hi I'm trying to get more into LINQ-to-XML, so I've made myself a neat little example XML-document to try things out on. In addition, I tried (and successfully) made my own XML-schema for that file, just to test things out. The XML-document is pretty straightforward, and pretty much looks like this: <cars xmlns="/carsSchema.xsd"> <ca...

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...

Check if KeyValuePair exists with LINQ's FirstorDefault

I have a dictionary of type Dictionary<Guid,int> I want to return the first instance where a condition is met using var available = m_AvailableDict.FirstOrDefault(p => p.Value == 0) However, how do I check if I'm actually getting back a KeyValuePair? I can't seem to use != or == to check against default(KeyValuePair) without a co...

Linq-to-entities - Include() method not loading

Hi If I use a join, the Include() method is no longer working, eg: from e in dc.Entities.Include("Properties") join i in dc.Items on e.ID equals i.Member.ID where (i.Collection.ID == collectionID) select e e.Properties is not loaded Without the join, the Include() works Lee ...

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...

Discrepancies between “Mock” Database and “Real” database behaviours.

We use C# and Linq2SQL with a MS SQL Server Database. We have a mockdatacontext to carry out some unit testing. When testing we have discovered two different behaviours depending on whether the "real" or "mock" database is used. Scenario 1: Real Database There are 5 records in the database: db = realDatabase db.InsertOnSubmit(new re...

Is a lambda expression from this possible?

I know from f in list where f.bar == someVar select f can be written as list.Where( f => f.bar == someVar ); Can a similar expression be created from from f in foo from b in f.bar where b.something == someVar select b; ? ...

WPF DataGrid Take() not working for ItemsSource

Hi, I am trying to bind a DataGrid to an Array for testing purposes. As long as I am not trying to filter anything, the auto columns work nicely. As soon as I try to filter the array by .Take(5) or any other filter, the rows stay empty, and there are only thing horizontal lines. I think it may have something to do, with the "anonymous" ...