不能从类型 void 隐式转换为 Systems.Collections.Generics.List<>

cannot implicitly convert from type void to Systems.Collections.Generics.List<>

我正在尝试使用 string.Equals 将新对象添加到对象列表中,但出现如下转换错误

这是我的对象:

public class TestNode : CSVBaseModel
{
    public int DatabaseID { get; set; }
    public string EntityType { get; set; } //make object
    public string Name { get; set; }
    public int StepNumber { get; set; }
    public virtual Job JobModel { get; set; }
    public TestNode(int databaseID, string entityType, string name, int stepNumber)
    {
        DatabaseID = databaseID;
        EntityType = entityType;
        Name = name;
        StepNumber = stepNumber;
    }
}

还有我的名单:

List<TestNode> testNodes = new List<TestNode>();

这里是错误发生的地方(在冒号之后,在语句中):

job.TestNodes = string.IsNullOrEmpty(columns[2]) ? job.TestNodes : testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));

谁能告诉我为什么它不能转换?任何帮助将不胜感激。

编辑:

TestNodes 是一个列表属性 对另一个对象通用。 testNodes 是特定于正在编译的 byteArray 的列表。我希望这是有道理的。也许这提供了更好的上下文:

private Job ProcessFile2(byte[] filestream)
{
    List<TestNode> testNodes = new List<TestNode>();
    Job job = new Job();
    Guid uniqueRecordId = Guid.NewGuid();
    StreamReader streamReader = new StreamReader(new MemoryStream(filestream), System.Text.Encoding.UTF8, true);
    DateTime creationDate = DateTime.Now;
    int nodeCounter = 0;

    string line;
    int linecounter = 0;
    while ((line = streamReader.ReadLine()) != null)
    {
        if (linecounter > 1)
        {
            var columns = line.Split(",");
            job.JobName = string.IsNullOrEmpty(columns[1]) ? job.JobName : columns[1];
            job.TestNodes = string.IsNullOrEmpty(columns[2]) ? job.TestNodes : new List<TestNode> { new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0) };
            job.TestNodes= string.IsNullOrEmpty(columns[3]) ? job.TestNodes : testNodes.Add(new TestNode(nodeCounter += 1, "OrganisationType", job.OrganisationType, 0));
            job.ContractingOrganisationType = string.IsNullOrEmpty(columns[4]) ? job.ContractingOrganisationType : columns[4];
            job.TestNodes = string.Equals(columns[0], "Job Step") ? testNodes.Add(new TestNode(nodeCounter += 1, "StepName", columns[6], Convert.ToInt32(columns[5]))) : job.TestNodes;
            job.TestNodes = string.Equals(columns[0], "Custom Input") ? testNodes.Add(new TestNode(nodeCounter += 1, "InputName", columns[6], Convert.ToInt32(columns[5]))) : job.TestNodes;
            job.TestNodes = string.Equals(columns[0], "Custom Output") ? testNodes.Add(new TestNode(nodeCounter += 1, "OutputName", columns[6], Convert.ToInt32(columns[5]))) : job.TestNodes;
            job.TestNodes = string.Equals(columns[0], "Generic Input") ? testNodes.Add(new TestNode(nodeCounter += 1, "GenericInputName", columns[6], Convert.ToInt32(columns[5]))) : job.TestNodes;
            job.Record = uniqueRecordId;
            job.CreationDate = creationDate;
            job.ModificationDate = creationDate;
            job.CreateBy = "System";
            job.ModifyBy = "System";
        }
        linecounter++;
    }
    return job;
}

你对三元运算符的使用...

job.TestNodes = string.IsNullOrEmpty(columns[2]) ? job.TestNodes : testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));

...这样有效地工作...

if (string.IsNullOrEmpty(columns[2]))
{
    job.TestNodes = job.TestNodes;
}
else
{
    job.TestNodes = testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
}

List<>.Add() method 有这个签名...

public void Add (T item);

void 表示它没有 return 任何东西。您不能将 void 方法的结果分配给其他存储,因为没有可分配的结果。 (请注意,这不同于 return null 的方法。)

要解决此问题,请分别执行对 Add() 的调用和随后的 List<> 赋值...

if (string.IsNullOrEmpty(columns[2]))
{
    job.TestNodes = job.TestNodes;
}
else
{
    testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
    job.TestNodes = testNodes;
}

由于 if 块的 true 分支实际上是空操作,我们可以将其重写为...

if (!string.IsNullOrEmpty(columns[2]))
{
    testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
    job.TestNodes = testNodes;
}

根据 job.TestNodes 的分配方式,可能需要改为...

job.TestNodes = testNodes;
if (!string.IsNullOrEmpty(columns[2]))
{
    testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
}