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 sampleDataTable.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;
In the first example you want to select three properties but the return type must be
IEnumberable<T>for some type T. Withselectyou are only allowed to select oneTper 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
Tcan be the type of the property so you don’t need to wrap it in an anonymous type. The second example would work with
newtoo, it just isn’t necessary.