linq

Make linq subquery

Registered table has stucture regid userid var Registered = form r in dc.registered where ... // list contains userid as f.k. I want user list who has registered. so query like var user = from u in dc.users where u.userid in // should contains in Register.userid I want to learn more about linq where I can find best stuffs? ...

linq to XML (C# to VB.net conversion)

What is equivalent of below in VB.net var list = (from x in xd.Descendants("product").Attributes("title") select new { Title= x.Value}).ToList(); VB.net Dim list = (From x In xd.Descendants("product").Attributes("title") _ Select New ( ??? )).ToList() Thanks ...

linq to xml

want to query below xml <courseList filedUnder="Courses"> <category code="4wd" title="4wd Operation"> <product code="lr" title="4wd Bush Driving"> <duration>1 Day</duration> </product> <product code="hr" title="4wd Defensive Driving"> <duration>1 Day</duration> </product> <product code="sr" title="Self-Recovery"> <durati...

Disjoint Union in LINQ

I have two sets (ILists) where I need all the items from the 1st list, where the item is not in the second list. Can anyone point me to the best way of achieving this with a LINQ statement? ...

Linq sub list Problem

I am very new to Linq and I am having trouble converting the following to a linq expression: Dim returnedInstructions As List(Of Integer) = New List(Of Integer) For Each j As Job In response.Jobs For Each i As Instruction In j.Instructions returnedInstructions.Add(i.InstructionId) ...

How can I convert anonymous type to strong type in LINQ?

I have an array of ListViewItems ( ListViewItem[] ), where I store a SalesOrderMaster object in each ListViewItem.Tag for later reference. I have some code that right now, goes through each ListViewItem safely casts the .Tag property into a SalesOrderMaster object, then adds that object to a collection of SalesOrders, only after checkin...

How to combine Where clause and group by in LINQ

Hi All, I am learning LINQ and got stuck here :-(. Can any one help me converting below code to LINQ? Select Catg,Count(*) From Mycatg where IsPublic=1 or FirstName='XXX' Group By Catg . ...

Is there a statement to prepend an element T to a IEnumerable<T>.

For example: string element = 'a'; IEnumerable<string> list = new List<string>{ 'b', 'c', 'd' }; IEnumerable<string> singleList = ???; //singleList yields 'a', 'b', 'c', 'd' ...

need a suggestion for debugging LINQ IMultipleResults

I've got a stored procedure that returns two possible result sets. If successful, it returns a 0 (int). If it fails it returns -1 and a table containing a list of errors. I'm trying to figure out how to deal with this using LINQ. I found Scott Guthrie's post about this, but he was using results that were mapped to types/tables. I had t...

linq to xml - get rid of blank xmlns

Hi, I'm trying to get rid of empty namespace tags in my xml file. All of the solutions i've seen are based creating the xml from scratch. I have various xelements constructed from a previous xml. All I'm doing is XElement InputNodes = XElement.Parse(InputXML); m_Command = InputNodes.Element("Command"); and it adding the xmlns = "" ev...

Is there a way to capture the index value in a LINQ Where method in C#?

My following C# code is obviously a hack so how do I capture the index value in the Where method? string[] IntArray = { "a", "b", "c", "b", "b"}; int index=0; var query = IntArray.Where((s,i) => (s=="b")&((index=i)==i)); //"&" and "==i" only exists to return a bool after the assignment o...

Get Directory Structure using Linq?

Hi I have an app where I get a list of drives on the computer and when you select one I populate a tree control with the directories and files. I was thinking of trying this using linq to loop round each directory and get its child directories. Has anyone done anything like this before? Thanks ...

Best practices and/or advice for diamond relationing tables (Linq to SQL)

I have to import the content of 4 tables from an old database into SQL 2005 for easier reporting. Products contains the id and product name, ProductProperties contains a variable number of properties for each product, Ingredients contains a variable number of ingredients for each product, and IngredientProperties contains the same prope...

LINQ Query Syntax to Lambda

Wondering if there is any way to get the lambda expressions that result from a LINQ "query" syntax expression. Given: var query = from c in dc.Colors where c.ID == 213 orderby c.Name, c.Description select new {c.ID, c.Name, c.Description }; Is there any way to get the generated "lambda" code / expr...

LINQ OrderBy help needed

Hi - I'm trying to create a collection of strings in an order defined by another array. Sounds confusing I know so let me explain requiredOrderOfElements{ [0] category1, [1] categoryX, [2] something else } my client passes up a string array containing the key and value ie passed from client { [0][0] categoryX, [0][1] value from Catego...

System.Data.Linq.ChangeConflictException: Row not found or changed.

I am trying to delete a selected gridview row using LINQ (No LINQDataSource). when the selection is changed, the detailsview binding is changed also. I can add a new entry to the database, but when i added this code to a delete button inside the updatePanel, i got an exception: try { var query = from i in db.QuestionModules where i.Que...

Is there a way to get the difference between two sets of objects in c#

I want to get the difference between two sets of ints in c#. Given s1 and s2 I want to return those ints which are in s1 and not in s2. I can do something such as: List<int> s1 = new List<int>(); List<int> s2 = new List<int>(); foreach (int i in s1) { if (s1.Contains(i)) { // } ...

Switching to LINQ

I'm considering spending time learning and using LINQ to SQL but after years of best practices advising NOT to embed SQL I'm having a hard time changing paradigms. Why does it seem accepted now to embed queries in compiled code? It seems almost a step backwards to me in some ways. Has anyone had issues with fix query / compile / deploy...

How do you catch a Save or Load event on a Linq-to-SQL class?

I have a Linq-to-SQL class, and I'd like to perform some pre-save validation before the record is persisted to the DB. In addition, once it has been saved, I'd like to have some post-save processing code. Similarly, when a record is deleted, I'd like to have pre- and post- methods that will be called, no matter from where the context.S...

C# LINQ - reading an XML

Hi, i need to store all the informationen from the xml in an array. My code doesn't work, because I always get just the first item from the xml. Does anyone know how to fix this? XDocument xdoc = XDocument.Load("http://www.thefaxx.de/xml/nano.xml"); var items = from item in xdoc.Descendants("items") ...