linq

How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load().

I'm new to using LINQ to Entities (or Entity Framework whatever they're calling it) and I'm writing a lot of code like this: var item = (from InventoryItem item in db.Inventory where item.ID == id select item).First<InventoryItem>(); and then calling methods on that object like this: var type = item.ItemTypeRe...

Clever tricks to find specific LINQ queries in SQL Profiler

Profiling LINQ queries and their execution plans is especially important due to the crazy SQL that can sometimes be created. I often find that I need to track a specific query and have a hard time finding in query analyzer. I often do this on a database which has a lot of running transactions (sometimes production server) - so just ope...

Determine the source DataContext for a Linq to Sql query

In my application I have several DataContexts that connects to different databases with different schemas. In a custom user control I display the results of the query and let the user edit them, and when the user edits the data I want to persist the changes to the database. To do that I need a reference to the source DataContext (or at l...

Optimize Group By in LINQ to Entities

I have this query in LINQ to Entities. var query = (from s in db.ForumStatsSet where s.LogDate >= date1 && s.LogDate <= date2 group s by new { s.Topic.topicID, s.Topic.subject, s.Topic.Forum.forumName, s.Topic.datum, s.Topic.Forum.ForumGroup.name, s.Topic.Forum.forumID } into g ...

Is yield useful outside of LINQ?

When ever I think I can use the yield keyword, I take a step back and look at how it will impact my project. I always end up returning a collection instead of yeilding because I feel the overhead of maintaining the state of the yeilding method doesn't buy me much. In almost all cases where I am returning a collection I feel that 90% of t...

Existing LINQ extension method similar to Parallel.For?

The linq extension methods for ienumerable are very handy ... but not that useful if all you want to do is apply some computation to each item in the enumeration without returning anything. So I was wondering if perhaps I was just missing the right method, or if it truly doesn't exist as I'd rather use a built-in version if it's availab...

Getting an Average over Multiple Collections in LINQ

I have a collection of collections, all the child collections have the same number of elements. The parent collection does nothing other than hold the child collections. [0] [Child_0] [ID: 1] [0] [Child_0] [Amount: 4] [0] [Child_1] [ID: 2] [0] [Child_1] [Amount: 7] [1] [Child_0] [ID: 1] [1] [Child_0] [Amount: 2] [1] [Child_1] [ID: 2] [1...

BindingList and LINQ?

I am new with Linq and I would like to sort some data that are in the BindinList. Once I did my Linq query, I need to use back the BindingList collection to bind my data. var orderedList = //Here is linq query return (BindingList<MyObject>)orderedList; This compile but fail in execution, what is the trick? ...

How do you use LINQ with Sqlite

Would someone explain how to get LINQ working with Sqlite. ...

Extract a Byte[] from an XElement with Linq to Xml

Hi, I am saving some small images to Xml as a Byte[] via the following XElement construct.. XElement xe = new XElement("Images", from c in qry select new XElement("Image", new XAttribute("Date", c.Date), new XElement("Data", c.Bytes))); the Bytes property is a Byte[], looking at the resulting elem...

LINQ grouping/subquery to fill a hierarchy data strcuture

I have a DataTable that queries out something like below usergroupid...userid......username 1.............1...........John 1.............2...........Lisa 2.............3...........Nathan 3.............4...........Tim What I'm trying to do is write a LINQ statement that will return an array of UserGroup instances. The UserGroup class h...

binding to linq inherited types in asp.net

Hi I have a datammodel in linq to SQL that includes a type "Party" which is sub classed twice for "Company" and "Individual". I am trying to bind two repeaters to linq to sql queries as follows Dim oComp As IEnumerable(Of Company) Dim oInd As IEnumerable(Of Individual) oComp = From oP As Company In ERM.Parties _ ...

Using Linq to create crosstab results

Hi, Im wondering if its at all possible to create crosstab style results with Linq. I have some data that looks like the following: var list = new[] { new {GroupId = 1, Country = "UK", Value = 10}, new {GroupId = 1, Country = "FR", Value = 12}, new {GroupId = 1, Country = "US", Value = 18}, new {G...

How to merge a collection of collections in Linq

I would like to be able to fusion an IEnumerable<IEnumerable<T>> into IEnumerable<T> (i.e. merge all individual collections into one). The Union operators only applies to two collections. Any idea? ...

Write stored procedures in Linq/Lambda (Unit-testable but performant stored procs). Possible???

I am creating a data source for reporting model (SQL Server Reporting Services). The reports requires a lot of joins and calculations (let's say, calculating financial parameters like money spent on this, that, amount A vs amount B)...all this involves subobjects. It makes a lot of sense to me to write unit tests for this code (i.e. wal...

Linq query ("not in ") on datatable

Hi I would like to return all rows from TableA table that does not exists in another table. e.g. select bench_id from TableA where bench_id not in (select bench_id from TableB ) can you please help me write equivalent LINQ query. Here TableA source : Excel and TableB source : Database I am loading Excel sheet data into DataTable ...

DataTable Query

Hi, I am new to LINQ. I am trying to find the rows that does not exists in the second data table. report_list and benchmark both type are : DataTable. Both these datatables are being populated using OleDbCommand,OleDbDataAdapter. I am getting an error "Specified cast is not valid." in foreach ... loop. I would appreciate your help. ...

Ignoring accents in SQL Server using LINQ to SQL

How can I ignore accents (like ´, `, ~) in queries made to a SQL Server database using LINQ to SQL? UPDATE: Still haven't figured out how to do it in LINQ (or even if it's possible) but I managed to change the database to solve this issue. Just had to change the collation on the fields I wanted to search on. The collation I had was: SQ...

LINQ or XSLT to turn one Element into another in Visual Basic 9

Short version: Could anyone suggest or provide a sample in LINQ to XML for VB, or in an XSLT of how to change one XML element into another (without hardcoding an element-by-element copy of all the unchanged elements)? Background: I have an XML file, that I think is properly formed, that contains a root entry that is <collection> and ...

How to retrieve mapping table name for an Entity in Entity-framework in program

is there any way to retrieve mapping table name for an Entity in Entity-framework in program? I know you can use .ToTraceString() to get the command text and then extract the table name, but ToTraceString() method is very slow. is there any other way like using ObjectContext.MetadataWorkspace? Thanks ...