linq

If using LINQ to SQL is there any good reason to learn SQL queries/syntax anymore?

I do understand SQL querying and syntax because of previous work using ASP.NET web forms and stored procedures, but I would not call myself an "expert" in it. Since I have been using ASP.NET MVC and LinqToSql it seems that so much of the heavy lifting is done for me and encapsulated away at the SQL end that I'm questioning whether ther...

Using LINQ to SQL and chained Replace

I have a need to replace multiple strings with others in a query from p in dx.Table where p.Field.Replace("A", "a").Replace("B", "b").ToLower() = SomeVar select p Which provides a nice single SQL statement with the relevant REPLACE() sql commands. All good :) I need to do this in a few queries around the application... So i'm lookin...

LINQ: Select an object and change some properties without creating a new object

Using LINQ, if I wanted to perform some query and return the object from the query, but change only some of the properties in that object, how would I do this without creating a new object and manually set every property? Is this possible? Example: var list = from something in someList select x // but change one property ...

Explain this LINQ code?

I asked a question in which one of the response contained the following LINQ code: var selected = lstAvailableColors.Cast<ListItem>().Where(i => i.Selected).ToList(); selected.ForEach( x => { lstSelectedColors.Items.Add(x); }); selected.ForEach( x => { lstAvailableColors.Items.Remove(x);}); Can someone explain the above LINQ to a tota...

Linq to SQL - How to find the the value of the IDENTITY column after InsertOnSubmit()

I am using LINQ to SQL to insert simple data into a table WITHOUT a stored procedure. The table has a Primary Key ID column, which is set as an IDENTIITY column in both SQL Server and in my DBML. First I call InsertOnSubmit(); with data for a single row, and then I call SubmitChanges(); to commit the row to the db. But I think there m...

Why does a Linq Cast<T> operation fail when I have an implicit cast defined?

I've created two classes, with one of them having an implicit cast between them: public class Class1 { public int Test1; } public class Class2 { public int Test2; public static implicit operator Class1(Class2 item) { return new Class1{Test1 = item.Test2}; } } When I create a new list of one type and try t...

Need help converting a nested SQL statement to LINQ

I have a statement similar to this that I need to run in Linq. I started down the road of using .Contains, and can get the first tier to work, but cannot seem to grasp what I need to do to get additional tiers. Here's the SQL statement: select * from aTable where aTableId in (select aTableId from bTable where bTableId in ...

Linq to XML - way to write binary Data into XML

How to write binary data into XML using LINQ which is being read from registery. Eg: ήì\ÎQÝÜ$Ñ.Ì6Å·xxµ0©ÐQfô:S9}:&...... ...

Implicit operators, Linq and Lambda expressions can make code less readable. But what is more readable?

In answering this question http://stackoverflow.com/questions/808725/why-does-a-linq-castt-operation-fail-when-i-have-an-implicit-cast-defined I have found that there are a number of ways to implicitly cast between objects. Consider the following two classes: public class Class1 { public int Test1; } public class Class2 { pu...

How to TOP, ORDER BY and DISTINCT in the same query with Linq-To-Sql?

How to translate the following query to Linq? SELECT DISTINCT TOP 10 A.*,A.X+A.Y AS SUMXY FROM TABLE_A AS A INNER JOIN TABLE_B AS B ON A.ID = B.AID ORDER BY SUMXY I don't want to split in two queries. ...

What is a good way using LINQ To SQL to update multiple tables through a view?

I have a view and it's composed of two tables. I want to edit a value in each table through the view and save those changes but LINQ is throwing an error about not being able to edit two values on the same view. Does anyone know of a good workaround? Thanks ...

linq to sql OnLoaded() with SQL View?

I am trying to extend my Linq-to-Sql entity with a few extra properties. These are "calculated" properties based on data from the underlying SQL View. For example, think of having a Date of Birth field, which is used to calculate an extended Age field. I tried to extend my entity class by extending the OnLoaded() method. I get a comp...

reading CDATA with linq to xml

Hello Everyone, I have the following xml file and cannot seem to figure out how to get the value in the elements (which are buried in a CDATA). I am trying to use linq to xml for this. If someone knows how you would convert this into a "Product" object (assume we have a product object that has properties with the same names as the ele...

How mainstream is LINQ?

How mainstream is LINQ? ...

How to use LINQ to filter property of child collection using (.Any())

How do I do the following SQL in LINQ? I am actually using LINQ to NHibernate (but maybe it's not possible in NHibernate LINQ due to embedded lambda expression I think). But I want to know generally how to do it in LINQ. I’ve never came across this situation before. SELECT c.CustomerID, c.CustomerName --etc FROM Customers c INN...

Search for whole word with Linq to SQL

Hi, I'm using VB.NET and Linq to SQL to do search query. I'm using the Contains method of Linq to look up the input keyword. It's like this note.note_content.Contains(InputKeyWord). The problem is that If I search for the "cord" then "according" will also come up. I want the result to be matched with keyword only. How do I do to sear...

Foreach in Linq

Can I replace these foreach's with LINQ or delegates? foreach (List<Shopping.BasketPayment> payList in Basket.basketPayments) { foreach (var pay in payList) { if (pay.paymentMethod == "Money Order" { foreach (OrderItem orderItem in order.Items) { ...

Does "Select New" in linq trigger an evaluation / load?

I'm currently trying to create a class which implements IEnumerable<T> in order to construct a Hierarchy from a flat list of objects which have references to each other through a ParentId property. I'd like to write a fluent interface for this so I can do something like this IEnumerable<Tab> tabs = GetTabs(); IEnumerable<TabNode> tabNo...

Noob in linq, selecting the first record of each record with the same value

Hi guys, im trying my best to learn LINQ but im still having a hard time coding it. Like this one, suppose I have a dataset or a List maybe and the names or fields of the of the collection object are the column names. Id | Date |Mon |Tues |Wed |Thu |Fri |Sat |Sun |Count 1 | 01/05 |=1=|==1==|==1=|==1=| 1=|=0=|=0==...

Convert / Cast IEnumerable to IEnumerable<T>

I have a class (A web control) that has a property of type IEnumerable and would like to work with the parameter using LINQ. Is there any way to cast / convert / envoke via reflection to IEnumerable<T> not knowing the type at compile time? Method void (IEnumerable source) { var enumerator = source.GetEnumerator(); if (enumerat...