Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the eqivalent to a SQL "in" clause here?
    primarykey
    data
    text
    <p>I'm new to LINQ and I've been at this for hours now. I have a List&lt;> of objects where one of the objects properties is a List of selected categories. I also have, outside of the objects, a List representing a subset of categories and I want to return all objects which contain at least one category that is also in the subset as illustrated in the following pseudo code (not my actual code)</p> <pre><code>List&lt;string&gt; subset = cat, dog, mouse List&lt;myclass&gt; myclasses = {name:alphie, category:[cat,elephant]},{name:sally, category:[fish]}, {name:bob, category:[dog, mouse]} </code></pre> <p>In the above example I need to return alphie and bob since they both have at least one category that's in my subset. </p> <p>The only solution so far is to get a list of both and then use expensive foreach loops to go through and compare. I'm sure LINQ must provide a more efficient way to achieve the same?</p> <p>More details (I think my pseudo code is not detailed enough)</p> <pre><code>public class RadioProgram { ... private List&lt;string&gt; _category = new List&lt;string&gt;(); public List&lt;string&gt; Category { get { return _category; } set { _category = value; } } ... } public class Category { ... private string _categoryName = ""; private List&lt;Category&gt; _subCategories = new List&lt;Category&gt;(); public string CategoryName { get { return _categoryName; } set { _categoryName = value; } } public List&lt;Category&gt; SubCategories { get { return _subCategories; } set { _subCategories = value; } } ... } </code></pre> <p>I have a method, GetCategories(string parentCategory), that returns all child categoryNames as List. Each radioProgram.Category (yes, name needs to be refactored to plural) is itself a List and may contain zero, one or more categoryNames. I'm getting my master list of radioPrograms and I want to return a subset that contain where each one contains at least one categoryName that matches the set from GetCategories.</p> <p>I'm trying to avoid changing the architecture of the application (which is a potential solution) as it means a lot of refactoring of existing functionality AND I think this happens to be a good exercise for tackling and understanding LINQ.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload