[英]How to get data from linq query using select
I'm testing a simple LINQ select query and want to get two DateTime values from the table, but I'm doing something wrong here and need to know what I'm doing/thinking wrong? 我正在测试一个简单的LINQ select查询,并希望从表中获取两个DateTime值,但是我在这里做错了什么,需要知道我在做什么/认为有什么错误吗?
My query: 我的查询:
var test = from x in db.Projects where x.ID == 1 select x;
Then I try to get on of the values like this: 然后,我尝试使用以下值:
DateTime Date = test. ?????
Here I thought I should get a suggestion from the Intellisense after the dot to pick the value from the column StartDate
the table, but this isn't working. 在这里,我认为我应该从Intellisense那里得到一个建议,以便从表的
StartDate
列中选择值,但这是行不通的。
If you need multiple matches... 如果您需要多个比赛...
Are you sure that you have multiple Project
objects that have the same ID of 1
which your query currently suggests? 您确定您的查询当前建议有多个ID为
1
Project
对象吗? If that is the case, then your query should return all of the records that meet that constraint via the Where()
method : 如果是这种情况,那么您的查询应通过
Where()
方法返回满足该约束的所有记录:
// Get your Projects that meet your criteria
var test = db.Projects.Where(p => p.ID == 1);
If you need to access properties from these elements, you could either loop through them explicitly : 如果您需要访问这些元素的属性,则可以显式地遍历它们:
// Iterate through each match that was found
foreach(var t in test)
{
// Access your properties here
DateTime dt = t.YourDateProperty;
}
Or you could accomplish this using a Select()
statement to only pull the properties that you need : 或者,您可以使用
Select()
语句仅提取所需的属性来完成此操作:
// This will return a collection of Dates mapped from each element in your collection
var testDates = db.Projects.Where(p => p.ID == 1)
.Select(x => x.YourDateProperty);
If you only need a single match... 如果您只需要一场比赛...
If you only need to match a single element within your collection, you might consider using the First()
, Single()
or their equivalent FirstOrDefault()
and SingleOrDefault()
methods, which will return a single entity that you can use as expected : 如果只需要匹配集合中的单个元素,则可以考虑使用
First()
, Single()
或它们等效的FirstOrDefault()
和SingleOrDefault()
方法,这将返回可以按预期使用的单个实体:
// This will return the first Project with an ID of 1
var test = db.Project.FirstOrDefault(p => p.ID == 1);
// If it was successful
if(test != null)
{
// Then you can safely access it here
DateTime dt = test.YourDateProperty;
}
The only difference between the methods mentioned (normal vs OrDefault()
) is that the OrDefault()
methods will return null
if no matching elements are found, so they generally require a null check as seen above. 提到的方法之间的唯一区别(常规vs
OrDefault()
)是,如果找不到匹配的元素,则OrDefault()
方法将返回null
,因此,如上所述,它们通常需要进行null检查。
test
is going to be an enumeration ( IEnumerable<>
, IQueryable<>
, etc... many are applicable) of your Project
type. test
将是您的Project
类型的枚举( IEnumerable<>
, IQueryable<>
等等。许多适用)。 So if, for example, you want the first record, you might do this: 因此,例如,如果您想要第一条记录,则可以这样做:
DateTime Date = test.First().SomeDateProperty;
All of the data returned from your query is in test
. 您的查询返回的所有数据都在
test
。 It could be zero records, one record, many records, etc. 它可以是零个记录,一个记录,许多记录等。
this is such as: 如:
var date = test.FirstOrDefault();
DateTime? Date = date != null ? date.StartDate : null;
In test
you will have a collection which matches the condition x.ID == 1
. 在
test
您将有一个匹配条件x.ID == 1
的集合。 You should iterate through that collection and take your needed properties. 您应该遍历该集合并获取所需的属性。
I suggest you to use the syntax: var result = db.Projects.FirstOrDefault(x => x.ID ==1);
我建议您使用以下语法:
var result = db.Projects.FirstOrDefault(x => x.ID ==1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.