C# Linq IEnumerable Select问题-我可以在Select中完成所有这些吗?
我有一个简短的问题。我可以在select语句中执行所有这些逻辑吗C# Linq IEnumerable Select问题-我可以在Select中完成所有这些吗?,c#,linq,select,ienumerable,object-initializers,C#,Linq,Select,Ienumerable,Object Initializers,我有一个简短的问题。我可以在select语句中执行所有这些逻辑吗 var entries = atisDAO.GetPME(xl, null); response.Data.Detectors = new List<DetectorDetails>(entries.Select(pme => new DetectorDetails {ID = pme.PlaceNum.ToString()})); if(response.Data.Detectors.Any()) {
var entries = atisDAO.GetPME(xl, null);
response.Data.Detectors = new List<DetectorDetails>(entries.Select(pme => new DetectorDetails {ID = pme.PlaceNum.ToString()}));
if(response.Data.Detectors.Any())
{
response.Data.Detectors.ForEach(d =>{
int id;
if(int.TryParse(d.ID, out id))
{
var summaries = atisDAO.GetSummaryEntries(id);
if (summaries.Any())
{
var count = summaries.Sum(summary => summary.TODCount + summary.BFICount + summary.ViolationCount);
var detectionDate = summaries.Max(summary => summary.ReadDate);
d.Count = count.ToString();
d.DetectionTime = new DateTimeZone {
ReadDate = detectionDate.ToString(DATE_FORMAT)
, ReadTime = detectionDate.ToString(TIME_FORMAT)
};
}
}
});
}
var entries=atisDAO.GetPME(xl,null);
response.Data.Detectors=新列表(entries.Select(pme=>newdetectordetails{ID=pme.PlaceNum.ToString()}));
if(response.Data.Detectors.Any())
{
响应.数据.检测器.ForEach(d=>{
int-id;
if(内部锥虫(d.ID,外部ID))
{
var summaries=atisDAO.GetSummaryEntries(id);
if(summaries.Any())
{
var count=summary.Sum(summary=>summary.TODCount+summary.BFICount+summary.ViolationCount);
var detectionDate=summaries.Max(summary=>summary.ReadDate);
d、 Count=Count.ToString();
d、 DetectionTime=新日期时区{
ReadDate=detectionDate.ToString(日期格式)
,ReadTime=detectionDate.ToString(时间格式)
};
}
}
});
}
做一个选择,然后在列表中循环并修改我刚才选择的项目,感觉是不对的。我可以在select语句中执行所有这些操作吗
谢谢你的提示
干杯,~ck在圣地亚哥当然,为什么不呢?是什么阻止您在Select语句中使用ForEach中的代码更改新的DetectorDetails?这有助于您了解您要去的地方吗?我不能确定所有的数据类型都匹配并且将按原样编译,但这是一种尝试,可以将所有的逻辑放在
.Select()
中。当然,它可以提高到更好!请随意编辑此答案,使其更好地工作
response.Data.Detectors = atisDAO.GetPME(xl, null).Select(pme =>
new DetectorDetails{
ID = pme.PlaceNum.ToString(),
Count = atisDAO.GetSummaryEntries(int.Parse(pme.PlaceNum.ToString())).Count(), //some work needed here to ensure pme.PlaceNum is actually an number
DetectionTime = new DateTimeZone{
ReadDate = summaries.Max(summary => summary.ReadDate).ToString(DATE_FORMAT),
ReadTime = summaries.Max(summary => summary.ReadDate).ToString(TIME_FORMAT)
}
}
);
我明白了。我需要在投影中包含一个返回语句
var entries = atisDAO.GetPME(xl, null);
response.Data.Detectors = new List<DetectorDetails>(entries.Select(pme =>{
var details = new DetectorDetails { ID = pme.PlaceNum.ToString()};
var summaries = atisDAO.GetSummaryEntries(pme.PlaceNum);
if (summaries.Any())
{
var count = summaries.Sum(summary => summary.TODCount + summary.BFICount + summary.ViolationCount);
var detectionDate = summaries.Max(summary => summary.ReadDate);
details.Count = count.ToString();
details.DetectionTime = new DateTimeZone {
ReadDate = detectionDate.ToString(DATE_FORMAT)
, ReadTime = detectionDate.ToString(TIME_FORMAT)
};
}
return details;
}));
var entries=atisDAO.GetPME(xl,null);
response.Data.Detectors=新列表(条目.选择(pme=>{
var details=new DetectorDetails{ID=pme.PlaceNum.ToString()};
var summaries=atisDAO.GetSummaryEntries(pme.PlaceNum);
if(summaries.Any())
{
var count=summary.Sum(summary=>summary.TODCount+summary.BFICount+summary.ViolationCount);
var detectionDate=summaries.Max(summary=>summary.ReadDate);
details.Count=Count.ToString();
details.DetectionTime=新日期时区{
ReadDate=detectionDate.ToString(日期格式)
,ReadTime=detectionDate.ToString(时间格式)
};
}
退货详情;
}));
我无法获得正确的语法。VisualStudio一直在对我咆哮。