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...
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.
Source: stackoverflow.com
Related Articles
- dealing with Linq XML and complex queries
- How to tackle complex LINQ queries with outer join?
- Queries with complex filtering LINQ to XML c#
- MVC dealing with multiple LINQ Queries in a single View
- Complex Linq query dealing with articles, categories and versioning
- Error: "The specified LINQ expression contains references to queries that are associated with different contexts"
- Best practices for dealing with LINQ statements that result in empty sequences and the like?
- Automatically checking for NULL relationships with LINQ queries
- Linq slowness materializing complex queries
- LINQ Where clause with Contains where the list has complex object
- Dealing with Queries in a Repository Pattern with multiple concrete implementations?
- LINQ Queries with dynamic Order By
- Suggestions for designing complex LINQ code
- Is there a JS library that supports writing linq to sql queries with nodejs?
- Dapper parameterised queries with LINQ autogenerated types
- Issue with dynamic LINQ queries containing brackets
- LINQ Source Code Available
- Linq with where clause in many-to-many EF Code First object
- LINQ for comparing two lists with complex entities
- How to concat strings in LINQ while properly dealing with NULL values
- The LINQ expression contains references to queries that are associated with different contexts
- LINQ expression that contains references to queries that are associated with different contexts
- Build Any() with Expression Trees for LINQ Queries
- More complex queries in LINQ than SQL
- Handling large SQL queries with LINQ
- Linq2Sql - Storing Complex Linq Queries for future dynamic execuction - raw text - possible?
- Inconsistent return types of linq queries with orderby clauses
- Wrapping a complex linq query with a Try-Catch block and catching the correct Exceptions
- Can you use LINQ with in memory objects rather than SQL Server queries to improve performance?
- Linq SQL error with one-to-many relationship and orderby complex expression
- Apply inner expression filter on IQueryable
- Grouping by a field in DocumentDB
- Entity Framework Linq equals value or is null
- Windows Azure Tables, querying using Contains
- How to get the updated record by comparing two list using linq C#
- how to use group by function for DbFunctions.DiffMinutes
- How to check linq query result against Sql Server Database for data already exist or not?
- How to do partial search on multiple keys dictionary
- How can i add wrapper to the current xml while defining the keyvalue pair using c#
- Remove Duplicate Entries of 'Multicolumn'-List with LINQ
- Coverting Guid to string
- Linq to Entities - SQL "IN" clause
- How to convert a linq query to ToList() to use extended properties
- Linq-to-sql user generated predicate
- How to loop through viewBag with strongly typed data model
- C# SQL query blocks server memory
- "A cycle was detected in the set of changes" When trying to add a circularly linked list to the database
- Show Only 10 records in gridview
- SQL Query on Parent Include Child on EF
- LINQ GroupBy generated incomplete query