linq

What are the alternatives to using Expand in a LINQ to ADO.net Data Service Query?

I am wondering if there are any alternatives to using the Expand key word when performing an LINQ to ADO.net Data Services query. The expand method does get me the data I am interested in, but it requires me to know all of the sub-objects that I am going to be working with in advance. My absolute preference would be that those sub-obje...

LINQ to XML gets no data with schema set

Hi I am trying to get some XML data with LINQ, but running into a problem. I am using a schema, which is set in the attribute xmlns ... <CarsForSale xmlns="http://schemas.sharplogic.net/CarSales.xsd"&gt; <CarForSale> There are many CarForSale elements. When the schema is set and I do this... XElement doc = XElement.Load(HttpCont...

LINQ query to WebControl.Controls

I have three TextBox controls on the page <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox_TextChanged" TabIndex="1"> <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" OnTextChanged="TextBox_TextChanged" TabIndex="2"> <asp:TextBox ID="TextBox3" runat="server" AutoPostBack="...

Best LINQ Tutorial?

What is the best LINQ tutorial that you know of? I am starting to play with it, but I haven't found a really good source yet. Any ideas? ...

How do you add dynamic 'where' clauses to a linq query?

I've got a User table with a bitmask that contains the user's roles. The linq query below returns all the users whose roles include 1, 4 or 16. var users = from u in dc.Users where ((u.UserRolesBitmask & 1) == 1) || ((u.UserRolesBitmask & 4) == 4) || ((u.UserRolesBitmask & 16) == 16) ...

LINQ inner join betwenn Enumerable and DB Table

I'm trying to determine which records to delete from a database when a user submits a form. The page has two CheckBoxList one representing the records before modification and one after. I can easily get the selected values that need to be deleted like this... //get the items not selected that were selected before var oldSelectedItems =...

Object Initializer syntax to produce correct Json

I am attempting to use linq to shape list of data into a particular shape to be returned as Json from an ajax call. Given this data: var data = new List<string>(); data.Add("One"); data.Add("Two"); data.Add("Three"); And this code: ** Which is not correct and is what needs to be fixed!! ** var shap...

Linq to SQL Class Regeneration

I've been using this nifty LINQ to SQL tool for a data access layer in an asp.net project. I keep making changes to the underlying tables and in order for the data classes to recognize the change I have to delete and readd the table which has changed. Is there some shortcut to regenerating the data layer? ...

LINQ to SQL Table Dependency

If I have two tables... Category and Pet. Is there a way in LINQ to SQL to make the result of the joined query map to a another strongly typed class (such as: PetWithCategoryName) so that I can strongly pass it to a MVC View? I currently have Category and Pet classes... should I make another one? Maybe I missing something here. Can a...

LinqToSql referenced entities will throw NullReferenceException

I have a very interesting problem on my LinqToSql model. On some of my tables i have a references to other tables and in LinqToSql this is represented by a EnitiyRef class, when you are trying to access the references table LinqToSql will load the reference from the database. On my development machine everything worked fine (the referen...

What is more efficient for parsing Xml, XPath with XmlDocuments, XSLT or Linq?

I have parsed Xml using both of the following two methods... Parsing the XmlDocument using the object model and XPath queries. XSL/T But I have never used... The Linq Xml object model that was new to .Net 3.5 Can anyone tell me the comparative effeciantly between the three alternatives? I realise that the particular usage would ...

How do you handle arbitrary namespaces when querying over Linq to XML?

I have a project where I am taking some particularly ugly "live" HTML and forcing it into a formal XML DOM with the HTML Agility Pack. What I would like to be able to do is then query over this with Linq to XML so that I can scrape out the bits I need. I'm using the method described here to parse the HtmlDocument into an XDocument, but...

SQL command to LINQ (pivoting)

Hello! I'm moving DB from MySQL (used ODBC) to MS SQL and I want to "translate" SQL queries to LINQ. Can someone help me with this (it should SUM Charge column for every location and group result by months): SELECT sum(case when Location="Location1" then Charge else 0 end) as Location1, sum(case when Location="Location2" then Charge el...

How would you do a "not in" query with Linq?

I have 2 collections which have an Email property in both collections. I need to get a list of the items in the first list where the Email does not exist in the second list. With SQL I would just use "not in" but I do not know the equivalent in Linq. How is that done? So far I have a join, like... var matches = from item1 in list1 joi...

How do I LINQify this?

Is there any way to clean up this type of loop using LINQ? List<Car> result; List<string> makes; List<string> models; for (int i = 0; i < makes.Count() && i < models.Count(); i++) { result.Add(new Car() { Make = makes[i], Model = models[i] }); } Basically I'm looking for some way to collate multiple arrays of individu...

Learning LINQ: QuickSort

I took the plunge this afternoon and began studying LINQ, so far just mucking around with LINQ on collections. One of the first things I tried was to implement QSort. Now -- ignoring the fact that I could just use an ORDERBY and that this is a very silly qsort implementation -- what I came up with was this: public class lqsort { p...

How do I add to a list with Linq to SQL?

I have a table in the database that I'm retrieving using LINQ to SQL, and as a part of my processing I want to add to this list, then update the database with the new items + any changes I've made. What I thought I could do was this: var list = (from item in db.Table select item).ToList(); [do processing where I modify ite...

LinqtoSQL and problems

I am using link to sql, I have a connected set of objects. I start out and do a linq statement like this Dim L= from II in context.InventoryItems select II Dim L2 = L.tolist The second line was so that I could narrow down where the problem was occuring. When the second line is hit I get an error "The EntitySet is already loaded and t...

Linq to SQL: DataTable.Rows[0]["ColumnName"] equivalent

Consider this: var query = from r in this._db.Recipes where r.RecipesID == recipeID select new { r.RecipesID, r.RecipesName }; How would i get individual columns in my query object without using a for-loop? Basicly: how do I translate DataTable.Rows[0]["ColumnName"] into Linq syntax? ...

Can I specify my explicit type comparator inline?

So .NET 3.0/3.5 provides us with lots of new ways to query, sort, and manipulate data, thanks to all the neat functions supplied with LINQ. Sometimes, I need to compare user-defined types that don't have a built-in comparison operator. In many cases, the comparison is really simple -- something like foo1.key ?= foo2.key. Rather than c...