I'm new to LINQ and I'm doing a simple project to learn the features of the technology.
Currently I've got a static class that wraps an array of object (a kind of simple factory). Let's say it looks like the following:
public static class Factory
{
private static Item[] items = new Item[]
{
// items are created here
};
// ...
}
Now I can add some functions to my Factory that allow me to query the inner array, e.g.
public static Item GetByID(ItemID id)
{
var query =
from item in items
where item.ID == id
select item;
return query.First();
}
However, this requires me to modify the internals of the Factory class. Is there a way to write such queries from the 'outer world' instead ?
public class OtherClass
{
var result = from it in Factory select ...
}
?
Factory.WhicheverCollection.Where(item => item.id == whicheverid)What's the problem?IEnumerable. From memory, Linq does something odd and can act on anything with a GetIterator or something, but you probably shouldn't trust that.getwith a privatesetfor your property is definitely the way I'd do it. Interfaces declare intent to a rather large extent. If you want people thinking of aFactoryas a list, thenIEnumerableis a good choice. IMO, nothing namedFactoryshould ever be a list, because no one would ever expect that.