This is probably a basic question for some, but it affects how I design a piece of my program.
I have a single collection of type A:
IEnumerable<A> myCollection;
I am filtering my collection on 2 different criteria:
IEnumerable<A> subCollection1 = myCollection.Where(x => x.Count > 10); etc.
Now, I know that the .Where expression will return a new instance of IEnumerable, but does the new collection contain the same reference to an instance of type A that 'myCollection' references, or are new copies of type A created? If new instances of type 'A' are created, is there a way to say that 'subCollection1' references the same instances of A as 'myCollection' references?
Edit: To Add further clarification.
I am looking for a way so that when I make a change to an instance of 'A' in 'subCollection1', that it is also modified for 'myCollection'.
The type of sequence returned by a query is determined by the type of value returned by the select clause. LINQ select can return a sequence that contains elements created during the execution of the query.
Answers. There are only two return types for a Linq query. It's either a single concrete object or a single anonymous type object that is returned. It can also be a List<T> of concrete objects or anonymous type objects that are returned in a collection.
Language-Integrated Query (LINQ) makes it easy to access database information and execute queries. By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.
In LINQ, quantifier operators are used to returning a boolean value which shows that whether some or all elements satisfies the given condition.
The instances are the same if they are classes, but copies if they are structs/value types.
int, byte and double are value types, as are structs (like System.Drawing.Point
and self-defined structs). But strings, all of your own classes, basically "the rest", are reference types.
Note: LINQ uses the same rules as all other assignments.
For objects:
Person p1 = new Person(); p1.Name = "Mr Jones"; Person p2 = p1; p2.Name = "Mr Anderssen"; // Now p1.Name is also "Mr Anderssen"
For structs:
Point p1 = new Point(); p1.x = 5; Point p2 = p1; p2.x = 10; // p1.x is still 5
The same rules apply when using LINQ.
Actually it depends on the collection. In some cases, LINQ methods can return cloned objects instead of references to originals. Take a look at this test:
[Test] public void Test_weird_linq() { var names = new[]{ "Fred", "Roman" }; var list = names.Select(x => new MyClass() { Name = x }); list.First().Name = "Craig"; Assert.AreEqual("Craig", list.First().Name); } public class MyClass { public string Name { get; set; } }
This test will fail, even though many people believe that the same object will be returned by list.First()
. It will work if you use another collection "modified with ToList()
".
var list = names.Select(x => new MyClass() { Name = x }).ToList();
I don't know for sure why it works this way, but it's something to have in mind when you write your code :)
This question can help you understand how LINQ works internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With