score:0
Thx a lot Jon! Your code helped me a lot to learn this concept of Linq to XML Ok, I have made a query without any helper methods, which works fine. I have formated a bit unusal but it might help other noops like me who take a look here to understand what is going on:
List<XElement> i = XMarkXML.Element("site").Element("open_auctions").Descendants("open_auction")
.Where(x => (x.Element("bidder") != null )
&&
(x.Elements("bidder").Any(b => (b.Element("personref").Attribute("person").Value == "person540")))
&&
(
x.Elements("bidder").Where(
b1 => (b1.Element("personref").Attribute("person").Value == "person540")
).First().ElementsAfterSelf("bidder").Any(
b2 => (b2.Element("personref").Attribute("person").Value == "person1068")
)
)
).ToList();
When taking actions on Collections, it is important, that there is something inside, so it is important to check if Collection.Count() != 0. After that it is possible to do the query.
score:3
EDIT: Okay, edited again. I think this is okay, but even with your sample file it would be nice if you could give us a concrete example of inputs and expected outputs:
// Extension methods
internal static bool IsBidder(this XElement bidder, string person)
{
return (string) bidder.Element("personref")
.Attribute("person") == person;
}
internal static IEnumerable<XElement> ElementsAfterFirst
(this IEnumerable<XElement> source, XName name)
{
var first = source.FirstOrDefault();
if (first != null)
{
foreach (var element in first.ElementsAfterSelf(name))
{
yield return element;
}
}
}
var query = doc
.Descendants("open_auction")
.Where(x => x.Elements("bidder")
// All the person20 bids...
.Where(b => b.IsBidder("person20"))
// Now project to bids after the first one...
.ElementsAfterFirst("bidder")
// Are there any from person51? If so, include this auction.
.Any(b => b.IsBidder("person51"))
);
The second extension method can also be written as:
internal static IEnumerable<XElement> ElementsAfterFirst
(this IEnumerable<XElement> source, XName name)
{
var first = source.FirstOrDefault();
return first == null ? new XElement[0] : first.ElementsAfterSelf(name);
}
It changes the timing slightly, but it shouldn't make any difference in this particular case...
Source: stackoverflow.com
Related Query
- dealing with Linq XML and complex queries
- Queries with complex filtering LINQ to XML c#
- Complex Linq query dealing with articles, categories and versioning
- Linq to xml with missing nodes in the source XML and null-coalescing operator won't work
- Best practices for dealing with LINQ statements that result in empty sequences and the like?
- Get excel cell value with Row and Column Position through open xml sdk linq query
- How to combine and update two xml files with LINQ c#
- Wrapping a complex linq query with a Try-Catch block and catching the correct Exceptions
- Linq SQL error with one-to-many relationship and orderby complex expression
- C# - Linq optimize code with List and Where clause
- Speed difference between Linq to XML and Excel with a OledbConnection?
- Group and sort elements in an XML document with LINQ in C#
- IEqualityComparer with Linq to XML and Distinct() is not executed in code?
- Stubbing Code for Test With Linq Expressions and Lambdas
- C# LINQ use xml with case insensitive queries
- Insert into multiple tables (one to one relationships) with Linq queries and SQLite
- Querying XML with LINQ and using null in place of a particular xml attribute if it does not exist
- Write Dynamic LINQ queries for sorting and projecting with EF Core
- Is LINQ to XML order dependent and can you number results with it?
- Problem with LINQ to XML and Namespaces
- Parsing XML with pairs of elements using XDocument and LINQ
- Flatten xml with text and element nodes using LINQ to XML
- linq to xml with C# and VS2008
- Silverlight and RIA Services with LINQ-to-SQL DataContext - Unable to use client side linq queries
- how to fetch values from special attribute names with LINQ and XML
- How to dynamically build and store complex linq queries against a list of hierarchical objects?
- serialized property to complex type in dto with linq and automapper
- Refactoring LINQ to Entities queries with let variables and sub-queries
- LINQ and C# - Dealing with a potentially null parameter
- Dealing with a null datetime element within xml using linq
More Query from same tag
- xml XDocument from isolatedstorage, for use with LINQ
- ASP.NET Core 5 Blazor WASM, gRPC, Entity Framework Core 5: many-to-many results in stack overflow
- How to add children to an object graph in C#, given the parent?
- Linq version of SQL "IN" statement
- How to cast object to EntitySet with variable generic
- What XML related technologies I should know before learning LINQ to XML?
- Linq to entities to return a list that contains elements from another list
- Linq to sort lists of different kind
- Select Same Element on Second List By Order of Second List
- Linq Distinct on list
- Linq SelectMany error
- Filter from List<T> of List<T>
- Linq - unable to join table due to null value
- How to remove two objects with the same ID but different date values with LINQ to objects
- Linq expression for surrounding numbers in an array
- Canonical Function in Linq to Entities not working
- Linq to Entities - Increment a column using primary key
- can these foreach loops be combined into one?
- Is there any way to ignore column of an entity from cassandra database with .net driver
- SQL to LINQ Converter
- DataLoadOptions equivalent for LINQ to Entities?
- ProjectTo does not recognize parameterless constructor
- Unable to cast the type 'System.Nullable`1' to type 'System.Object' - ASP.NET MVC
- c# How to use linq to get xml string by filtering some data?
- Linq not getting correct value in List
- Get Values With One Select and Bind To View Model
- linq - convert nested foreach loop
- LINQ - Entity framework code first - Grouped results to custom class
- Select all Items which intersect
- Is there already a Conditional Zip function in c#?