linq

LINQ to XML Syntax

I've got a simple POCO class to hold data extracted from an XML file defined as follows: public class Demographics { public string FirstName { get; set; } public string LastName { get; set; } public string MiddleName { get; set; } public string Gender { get; set; } } I've got a fairly straight-forward XML file (or rath...

LINQ query question, needs joins

Not entirely sure of a good title for this, feel free to edit it to a good one I have an images object, which contains a list of organs which it is related to. I want to query a list of images and find which ones have all of the organs in a list of organs. Here is the method signature public static IEnumerable<Image> WithOrgans(this ...

It's not refreshing/databinding the page. How to refresh linq datasource?

protected void DetailsView1_ItemInserted(object sender, EventArgs e) { using (SqlCommand cmd2 = new SqlCommand("uspUpdateDisplayHours", cn)) { cn.Open(); cmd2.CommandType = CommandType.StoredProcedure; cmd2.ExecuteNonQuery(); cn.Close(); } DetailsView1.DataBind()...

How to enable the user to arbitrarily query IEnumerable collections?

Imagine an in-house application aimed at the very technical user. The app maintains data. There isn't a heck of a load of data, but there is enough to make some sort of a query mechanism necessary. The application's UI is very simple. It basically allows the user to enter a query (as text), and shows the data matching that query. Had t...

What is the right way to enumerate using LINQ?

Code: var result = db.rows.Take(30).ToList().Select(a => AMethod(a)); db.rows.Take(30) is Linq-To-SQL I am using ToList() to enumerate the results, so the rest of the query isn't translated to SQL Which is the fastest way of doing that? ToArray()? ...

Find matched directories using a list of regular expressions

I have an IEnumerable<DirectoryInfo> that I want to filter down using an array of regular expressions to find the potential matches. I've been trying to join my directory and regex strings using linq, but can't seem to get it right. Here's what I'm trying to do... string[] regexStrings = ... // some regex match code here. // get all ...

Initialize a Jagged Array the LINQ Way

I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using the traditional loop: var myArr = new double[rowCount][]; for (int i = 0; i < rowCount; i++) { myArr[i] = new double[colCount]; } I thought maybe some LINQ function would give me an elegant way to do this in one statement. However, the c...

ASP.NET: Trying to parse Google Base XML, can't access "g:" tags

My customer doesn't want a database but would prefer to update their data in an XML file. This is all well and good. However, they also want their items to be submitted to Google products. This complicates things a bit. I decided to attempt to just use the Google XML file for the database rather than create and maintain two separate file...

Faking IGrouping for LINQ

Imagine you have a large dataset that may or may not be filtered by a particular condition of the dataset elements that can be intensive to calculate. In the case where it is not filtered, the elements are grouped by the value of that condition - the condition is calculated once. However, in the case where the filtering has taken place,...

ASP.NET/XML/LINQ: Extract Attribute from Element

I have the following in an XML file: <entry> ... <link href="http://www.example.com/somelink" /> ... </entry> I want to extract the "href" attribute. I've been getting my list of elements like so: Dim productsDoc = XDocument.Parse(productXML.ToString()) Dim feed = From entry In productsDoc...<entry> Select entry For Each entry I...

LINQ - Select correct values from nested collection

Consider the following class hierarchy: public class Foo { public string Name { get; set; } public int Value { get; set; } } public class Bar { public string Name { get; set; } public IEnumerable<Foo> TheFoo { get; set; } } public class Host { public void Go() { IEnumerable<Bar> allBar = //Build up some large list //Get...

Caching when using Query Expressions?

I was reading an article about how query expressions defer executions. Does that mean when we have a collection like: IEnumerable<int> collection = from i in integers where i % 2 == 0 select i; It is gonna be recalculated every time the collection is accessed? If so, what's the general practice to deal with this? To convert into a ne...

LINQ: How to perform .Max() on a property of all objects in a collection and return the object with maximum value

Hi, I have a list of objects that have to int properties. The list is the output of anothre linq query. The object: public class DimensionPair { public int Height { get; set; } public int Width { get; set; } I want find and return the object in the list which has the largest Height property value. I can manage to get the high...

Linq to Sql structure standard

Hey, I was wondering what the "correct"-way is when using a Linq to Sql-model in Visual Studio. Should I create a new model for each of my components, say Blog, Users, News and so on and have all different xxxDataContext's with tables and SPROCs added in each of these. Or should I create one MyDbDataContext and always work against that...

linq - how combine conditions in a join statement

im working with xml and linq. I have 2 xml files both contain "ID" and "LANGUAGE" I want to do a join based on where the both the ID and LANGUAGE are equal in both files I have something like this: var data= from details in h_details.Descendants("ROW") join inst in instance.XPathSelectElements("//Row") on details.Element("ID").Value...

dbml entity relationship problems

I'm creating a .dbml file from a database. My "property" table has foreign keys to the "county" table and to a "propertysource" table. When code is generated, Property.Source is defined as a PropertySource type, but Property.County is defined as an int, instead of a County type. I'm afraid I don't have the experience with LINQ to SQL t...

Subsonic 3 Linq Projection Issue

OK I'm banging my head against a wall with this one ;-) Given tables in my database called Address, Customer and CustomerType, I want to display combined summary information about the customer so I create a query to join these two tables and retrieve a specified result. var customers = (from c in tblCustomer.All() ...

Linq, VB - Anonymous type cannot be converted to anonymous type.

I'm a Linq noobie, maybe someone can point me in the right direction. What's wrong here? These anonymous types seem to have the same signatures. '*** Get all of the new list items' Dim dsNewFiles = From l1 In list1 _ Where Not (From l2 In list2 _ Select...

sqlmetal.exe not found

Whenever i try to run sqlmetal, i get this: 'sqlmetal' is not recognized as an internal or external command, operable program or batch file this is from both CMD and the Visual Studio Command Prompt I've used sql metal many times on other machines, however it doesn't seem to work on this machine... Am i missing something? ...

Is it better to call ToList() or ToArray() in LINQ queries?

I often run into the case where I want to eval a query right where I declare it. This is usually because I need to iterate over it multiple times and it is expensive to compute. For example: string raw = "..."; var lines = (from l in raw.Split('\n') let ll = l.Trim() where !string.IsNullOrEmpty(ll) ...