如何在 ForEach() C# 中插入值
我有一个 foreach 循环,需要对其进行插值并将值添加到列表中。 我有需要从中选择的模型。
这是我用于选择的 POCO。
public class Data
{
public int UnixTime { get; set; }
public DateTime UnixDateTime { get; set; }
public float TestTime { get; set; }
public float AirTemp { get; set; }
public float RH { get; set; }
public float SurfaceTemp { get; set; }
public double LowFreq { get; set; }
}
编辑:
var userType = "RH"; // Statically typed here, but it gets passed in as a parameter on a method not included.
foreach (var item in GetListOfSensorData)
{
Two separate lists I add to.
unixTimeList.Add(item.UnixTime);
//I could hard code item.TestTime .. AirTemp .. etc
// I didn't want to do that. I want to concatenate or
// interpolate that string onto item
selectedDataList.Add(item. "interpolate here");
}
用户做出此选择。我不想做一堆 if 条件,所以我认为插值可以工作。
我尝试过:连接和插入值的多种方法。使用插值时我没有取回值。
我所希望的是: 当我对值 selectedDataList.Add(item.RH); 进行硬编码时我得到了我正在寻找的价值观 为了。当我插值时我没有得到结果。我基本上得到了一个连接字符串。
有没有办法在 foreach 循环中插入我正在寻找的项目?
我从插值中看到的屏幕截图。
与我对值进行硬编码时相比
I have a foreach loop that I need to interpolate and add values to a list.
I have model that I need to select from.
here are the POCOs I use for the selection.
public class Data
{
public int UnixTime { get; set; }
public DateTime UnixDateTime { get; set; }
public float TestTime { get; set; }
public float AirTemp { get; set; }
public float RH { get; set; }
public float SurfaceTemp { get; set; }
public double LowFreq { get; set; }
}
EDIT:
var userType = "RH"; // Statically typed here, but it gets passed in as a parameter on a method not included.
foreach (var item in GetListOfSensorData)
{
Two separate lists I add to.
unixTimeList.Add(item.UnixTime);
//I could hard code item.TestTime .. AirTemp .. etc
// I didn't want to do that. I want to concatenate or
// interpolate that string onto item
selectedDataList.Add(item. "interpolate here");
}
A user makes this selection. I didn't want to make a bunch of if conditions so I thought interpolate would work.
What I have tried: Multiple ways to concatenate and interpolate the value. I don't get the values back when using interpolation.
What I'm hoping for:
When I hard code the value selectedDataList.Add(item.RH); I get the values I'm looking
for. I don't get the results when I interpolate. I get a concatenated string basically.
Is there a way to interpolate the item I'm looking for in the foreach loop?
Screen shot of what I'm seeing from the interpolation.
Compared to when I hard code the value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论