Tag Archives: linq

how to add/insert conditional node into XML using linq to XML

Questions: I generated an xml file like this: XElement employees = new XElement(“Work”, new XElement(“record”, new XElement(“Name”, textBox1.Text), new XElement(“Phone”, “206-555-0144”), new XElement(“Address”, new XElement(“Street1”, “123 Main St”), new XElement(“City”, “Mercer Island”), new XElement(“State”, “WA”), new XElement(“Postal”, “68042”) )), new XElement(“record”, new XElement(“Name”, “22222”), new XElement(“Phone”, “353245345”), new XElement(“Address”, new XElement(“Street1”, “sdfain St”), new XElement(“City”,… Read More »

Using text SQL with LINQ

Questions: I’m currently having issues with using LINQ to SQL for my project. This is because most of the lengthy SQL queries (or views, rather) are hard-coded into C#, and what we’ve been doing all along is to use context.Database.SqlQuery<ClassName>(sql, sqlParams). It’s been generally very effective, but right now we need something more dynamic. We… Read More »

How to add OR condition in LINQ while querying XML?

Questions: Problem: return all records where Jon Doe =<bus_contact> OR <bus_sponsor>. Current code returns record 1, so what modifications are required to return records 1 and 2 using just one query?. XML <root> <row> <project_id>1</project_id> <project_name>Name1</project_name> <bus_contact>Jon Doe</bus_contact> <bus_sponsor>Bruce Wayne</bus_sponsor> </row> <row> <project_id>2</project_id> <project_name>Name2</project_name> <bus_contact>Peter Parker</bus_contact> <bus_sponsor>Jon Doe</bus_sponsor> </row> </root> C# class Program { static… Read More »

LINQ Select Distinct while ignoring the XML field

Questions: I have a complex LINQ query (using LINQ 2 EF) that can return duplicate results and I’m thus using the .Distinct() method to avoid duplicates. Here’s the skeleton: var subQuery1 = // one query… var subQuery2 = // another query… var result = subQuery1.Distinct().Union( subQuery2.Distinct() ).ToArray(); Each of the sub queries join a common… Read More »

LINQ to XML: What is the most effective way to move nodes up and down

Questions: I need to move sibling nodes before and after certain nodes. Here is the code im working with <tabs> <tab> <name>Overview</name> </tab> <tab> <name>Testing</name> </tab> <tab> <name>Performance</name> </tab> <tab> <name>Braking</name> </tab> </tabs> I would like to move the tab with testing in it above Overview. How would I go about this using linq to… Read More »