C# LINQ question, Why need new here?
Why we need the new
in the select
statement on one of them?
var runs = from sampleDataTable in db.SampleData
from sampleInfoTable in db.SampleInfo
where sampleDataTa开发者_开发百科ble.SampleInfo.SampleInfoId == sampleInfoTable.SampleInfoId
select new {sampleDataTable.Timestamp, sampleDataTable.SampleDataId, sampleInfoTable.Lane} ;
and
var runs2 = from sampleDataTable in db.SampleData
from sampleInfoTable in db.SampleInfo
where sampleDataTable.SampleInfo.SampleInfoId == sampleInfoTable.SampleInfoId
&& sampleDataTable.Timestamp == timestamp
select sampleInfoTable.Lane;
I believe new is required because you are creating a new anonymous type containing the columns and types in your select statement.
Because you're selecting a new anonymous type, not an individual property.
Example 1 will contain an IQueryable
of your anonymous type..
Each element will contain a type with the properties you defined. For example:
foreach (var result in resultset)
{
Console.Writeline(string.Format("{0}\n{1}\n{2}\n{3}\n",
result.TimeStamp,
result.SampleDataId,
result.Lane));
}
In the first example you want to select three properties but the return type must be IEnumberable<T>
for some type T. With select
you are only allowed to select one T
per item in the collection you are selecting from. So you have to create an object that contains the three properties and select the reference to that object. This is what new does here - it creates an object of an anonymous type containing the three properties that you requested.
In the second example you only need to select a reference to one thing. The type T
can be the type of the property so you don't need to wrap it in an anonymous type. The second example would work with new
too, it just isn't necessary.
Because you're creating a new instance of an anonymous type containing sampleDataTable.Timestamp
, sampleDataTable.SampleDataId
and sampleInfoTable.Lane
in the first case whereas in the second you're simply selecting a sampleInfoTable.Lane
.
As each time you iterate through your Linq recordset you will be selecting a New instance of an anonymous type containing the properties you are declaring within your linq select.
In the second one you are just selecting the Lane, so the compiler knows that run2
will be an enumerable of the type of Lane.
One the first one, you are selecting several fields, and the way the compiler does this is by creating a new class that has 3 properties, one of type timestamp (datetime?), another for datasampleId (int?), and another of type lane. You wont see this type of classes, they are compiler generated, and only work in the scope of a method (you cant return them or send them as parameters). The syntax to tell the compiler to do this is by using the new.
These types are called anonymous types
because you dont know what the name of the type is, only the compiler. (but if you use reflector or some other tool, you'll be able to see the classes, which usually have weird names that you would not be allowed to used using characters such as apostrophes)
Anonymous types are typically used in the select clause of a query expression to return a subset of the properties from each object in the source sequence
Anonymous types
are created by using the new operator with an object initializer.
Note : Here the anonymous type is used to set the properties of the corresponding fields(sampleDataTable.Timestamp, sampleDataTable.SampleDataId, sampleInfoTable.Lane
)
For the first one only one type is there so the new operator with an object initializer is not required. if it is there also no problem .
精彩评论