Hierarchy class Linq search

linq hierarchical query
linq select parent and child collection

I am trying to use Linq to search for a certain Product. How would I conduct this?

public class ShoppingCart : List<CartLine>
    // implement constructors you want available
    public ShoppingCart(){}

    public ShoppingCart( IEnumerable<CartLine> collection ) : base( collection ) {}

    public ShoppingCart( int capacity ) : base( capacity ) {}

public class CartLine
    public int CartLineId { get; set; }
    public Product Product { get; set; }
    public int Quantity { get; set; }

public class Product
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ImageLocation { get; set; }

    public int? ProductCategoryId { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }

ShoppingCart shoppingCart = new ShoppingCart();

Linq Query Search: This keeps giving me errors, trying to use Resharper to get this in my repository pattern, testing

 shoppingCart.Find(p=>p.CartLine.Product.ProductName) = "SamsungTV"

In the line shoppingCart.Find(p=>p.CartLine.Product.ProductName), p is already a CartLine. You also need to compare ProductName inside the query. Try

CartLine cartLine = shoppingCart.Find(p => p.Product.ProductName == "SamsungTV");

Using LINQ to Query Object Hierarchies. I used LINQ to solve the following problem: find all titles of objects at hierarchy level X when you know object ID in hierarchy level Y. I cannot imagine if there is some other solution that is same short and clear as one that LINQ provides. Take a look and decide by yourself. So, the simplest option is to write a function that traverses your hierarchy and produces a single sequence. This then goes at the start of your LINQ operations, e.g. IEnumerable<T> Flatten<T>(this T source) { foreach(var item in source) { yield item; foreach(var child in Flatten(item.Children) yield child; } }

For LINQ can u use a "where" :

var result =  shoppingCart.Product.Where(x => x.ProductName == "SamsungTV").ToList();

now you will have as a list of all in "Result" only where product name is Samsung TV ,to do what you like with it.

That's why we have one class per level. Also the number of levels is fixed and there is no plan to expand this hierarchy. By the way, you can  It has to read and return one element at the time, like other LINQ methods. User has to be able to specify a traverse type – the same input can return different results for different traverse types. The extension method itself is quite simple: publicstaticIEnumerable<T>Traverse<T>(thisTsource,Func<T,IEnumerable<T>>childrenSelector,TraverseTypetype){if(childrenSelector==null){thrownewArgumentNullException("childrenSelector");}switch(type){caseTraverseType.

If you want to find instance of CartLine that' product name is "SamsungTV" you could use the following expression:

CartLine cartLine = shoppingCart.Find(p => String.Equals(p.Product?.ProductName, "SamsungTV", StringComparison.OrdinalIgnoreCase));

It will find product using ordinal (binary) sort rules and ignoring the case of product name. If you want to use another string comparison rules, define it using StringComparison enumeration.

It's about LINQ again, but it's no as low-level as the one from This time the question is quite simple: How to search Hierarchical Data with Linq. start with some sample data we could fight against, starting with Person class:. To implement inheritance mapping in LINQ, you must specify the attributes and attribute properties on the root class of the inheritance hierarchy as described in the following steps. Developers using Visual Studio can use the Object Relational Designer to map inheritance hierarchies. See How to: Configure inheritance by using the O/R Designer.

Querying XML for Content Type We can use LINQ to XML to query not only for 3-2 carefully to better understand the hierarchy between LINQ to XML classes. This extension method can be used to turn any hierarchical data into a flat list, which can them be searched using LINQ. Another great thing about this solution is that is uses lazy evaluation, thus it only does as much work as the caller demands. For example, in the above code, Flatten will stop churning out items as soon as a HighScore is found.

Classes with One-To-Many Relationship. In this article, I will demonstrate how to query a Hierarchical Parent-Child Structure using LINQ. If you want to maintain Linq like syntax, you can use a method to obtain all the descendants (children + children's children etc.) static class NodeExtensions { public static IEnumerable<Node> Descendants(this Node node) { return node.Children.Concat(node.Children.SelectMany(n => n.Descendants())); } }

If you want to, you can write LINQ queries that return flat, SQL-like Consider the following simple hierarchical query that captures the A Customer class is connected to a set of orders in a clearly defined hierarchical  In this article, I will demonstrate how to query a Hierarchical Parent-Child Structure using LINQ. Note: You may also want to read some more LINQ Tutorials written by me to get familiar with LINQ To print the results, I am using a utility class called ObjectDumper which can be obtained from here .

  • Hi! In first, the predicate that is used in linq expression is wrong: it must return a boolean result. I think you want to use shoppingCart.Find(p=>p.CartLine.Product == "SamsungTV"). But Product property of a CartLine instance is not string. See documentation to use the method properly.
  • What are you trying to do? search for Product with name "SamsungTV"?
  • you got it I believe, shoppingCart.Find(p => p.Product.ProductName == "SamsungTV"); Thanks
  • This is not linq. The Find method belongs to the List<T> class. Also, you should read Eric Lippert's answer to Why not inherit from List<T>? And another thing - you can simply use a Dictionary<Product, int> to store the cart, where the int stands for quantity, and remove the CartLine class.
  • so many good suggestions, I was reading from here, he has Cartlineid, but thanks for the input github.com/Apress/pro-asp.net-core-mvc-2/blob/master/…