我有这门课Variables
public class Variables
{
public enum DataType
{
INT, CHAR, BOOL, FLOAT
}
public string Name { get; set; }
public DataType Type { get; set; }
public object Value { get; set; }
public Variables(string name, DataType type, object value)
{
Name = name;
Type = type;
Value = value;
}
}
还假设Name
是唯一的。在另一个类中,我有一个变量列表,List<Variables>
public void Main()
{
List<Variables> varList = new List<Variables>();
varList.Add(new Variables("x",DataType.BOOL, true));
varList.Add(new Variables("y",DataType.BOOL, false));
varList.Add(new Variables("z",DataType.BOOL, true));
varList.Add(new Variables("a",DataType.INT, 1));
varList.Add(new Variables("b",DataType.INT, 10));
// I need this to convert into LiNQ.
foreach (Variables _x in varList)
{
if (_x.Name == "z")
{
_x.Value = false;
}
}
}
我怎样才能转换foreach
成LiNQ
?我试过了.Where
,.Select
但他们回来了IEnumerable
。是否有一种LinQ
方法只返回与给定条件匹配的单个值?例如,
Variables _var = varList.MethodName(LambDa Expression).....