score:1
Accepted answer
enum materialtype
{
none,
wood,
glass,
steel,
cloth
}
class ingredient
{
public ingredient(materialtype type, int amount)
{
type = type;
amount = amount;
}
public materialtype type { get; }
public int amount { get; }
}
class recipe
{
public recipe(string name, params ingredient[] ingredients)
: this(name, (ienumerable<ingredient>) ingredients)
{
}
public recipe(string name, ienumerable<ingredient> ingredients)
{
name = name;
ingredients = ingredients.toarray();
}
public string name { get; }
public ingredient[] ingredients { get; }
}
[testclass]
public class findallitemsfixture
{
private readonly static ienumerable<recipe> allitemrecipes = new list<recipe>
{
new recipe("sword", new ingredient(materialtype.steel, 3)),
new recipe("spear", new ingredient(materialtype.steel, 1), new ingredient(materialtype.wood, 3)),
new recipe("table", new ingredient(materialtype.wood, 6)),
new recipe("chair", new ingredient(materialtype.wood, 4)),
new recipe("flag", new ingredient(materialtype.cloth, 2)),
};
ienumerable<recipe> getallrecipesusingmaterial(materialtype materialtype)
{
return allitemrecipes.where(r => r.ingredients.any(i => i.type == materialtype));
}
[testmethod]
public void getallwoodenrecipes()
{
var expectednames = new string[] { "spear", "table", "chair" };
var woodenitems = getallrecipesusingmaterial(materialtype.wood);
collectionassert.areequal(expectednames, woodenitems.select(i => i.name).toarray());
}
[testmethod]
public void getallclothrecipes()
{
var expectednames = new string[] { "flag" };
var clothitems = getallrecipesusingmaterial(materialtype.cloth);
collectionassert.areequal(expectednames, clothitems.select(i => i.name).toarray());
}
}
score:1
var woodrecipes = recipes.where(r => r.requireditems.contains(wood)).tolist();
Source: stackoverflow.com
Related Query
- Linq code to get the index of an object in an array from an object within a list
- Get the current index position in a list when creating a new object in a linq expression
- Get array of double from list of array of double, where first value in array is the max first value using linq
- How to select values within a provided index range from a List using LINQ
- Get index of object in a list using Linq
- Linq - Get the Index of the Last Non-Zero Number of Array
- How can I get LINQ to return the index of the object which has the max value in a collection?
- Get array index values of the top 1000 largest entries inside an array using LINQ
- Get the count of distinct elements from a list of lists using LINQ
- Linq select strings from list when condition is met and save the index
- Using LINQ to get the difference between two list of objects based only on a single object property
- Linq to return a new object with a selection from the list
- Using LINQ to get a list of items where the item contains a part of an item from another list
- What is the best way to get the percentage for an object in linq list and map it to JSON?
- Get minimum and maximum time value from list of object property using Linq
- Linq query to get an object from list of lists
- Get the objects with the maximum value for a specific property from a list using LINQ
- How to get the index of int array when using linq query
- How to get the second repeated item from a collection of objects using LINQ to object
- What is the correct way of retrieving a given business object from a list of business objects using LINQ Where vs. Find?
- Get the number from list using LINQ with specific conditions
- Get the index of a partially matching item in a list c# using linq
- Get items from list where index not equal to an int using LINQ
- Can someone tell me why this simple bit of c# code won't work, involves recursively calling method from within itself to get the root category ID
- How to grab the index from a list using LINQ
- Linq query to get the properties from object B which have the same name and type of object A
- Linq lambda for searching a List within a List, finding a match, then deleting it from the list
- Linq - Get all keys from dictionary where all values in the value list are equal to criteria
- Linq - Get items from the list based on "weight" and "order"
- LINQ get the best match from the list comparing strings
More Query from same tag
- Compile expression that requires a parameter
- The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable.
- Dynamically Modifying a LINQ to SQL Select Statement's Columns
- Is it possible to get other collection than IEnumerable<T> when using LINQ: XML to object?
- pass multiple data as a list to View in MVC
- c# lambda expression using Sum for multiple comparisons
- LINQ query bool value to pass in a different Int
- LINQ query to find items in a list containing substring elements from a second list
- Take all elements of .Split (' ') and take last OR exclude last item
- Left Outer Join using DefaultIfEmpty throws NullReferenceException
- Marking lambda expression with async keyword
- C# How to turn <T> and Expression<Func<T, string>> selector into T.Select( selector)?
- LINQ and ReSharper
- Dictionary Values to List from HashSet of Keys
- Remove items from an IList<Log> where log.id exists in IList<short>
- LINQ vs foreach vs for performance test results
- How can I compare two Lists and create another list where the match?
- C# Declare variable in lambda expression
- How can I convert this linq.expression from C# to VB?
- How to merge two classes into single unit?
- if else statement in XElement
- Why do I need a ToList() to avoid disposed context errors?
- MvcContrib Grid Sorting
- How to Bind an Image from an ObservableCollection to View
- How to remove comma characters from a semicolon delimited List<string>?
- Plain ArrayList Linq c# 2 syntaxes (need a conversion)
- Dyamic Linq .Net 4 error 'userid' could not be resolved in the current scope or contex
- Performance between check exists before add to list and distinct in linq
- how can i use array index in Linq expression?
- How to search string not in char array using LinQ