C# 如何在c中更新匿名列表中的值#

C# 如何在c中更新匿名列表中的值#,c#,C#,我有一个匿名类型的列表,其中有tagId,isTagSelected&tagName。我想要的是根据某些条件添加第四个属性 代码: 结果: {tagId = 1, isTagSelected = true, tagName = "a"} {tagId = 2, isTagSelected = false, tagName = "b"} {tagId = 3, isTagSelected = true, tagName = "c"} 我想要的是,如果标记名是a,则添加Size=“S”elsest

我有一个匿名类型的列表,其中有
tagId,isTagSelected&tagName
。我想要的是根据某些条件添加第四个属性

代码:

结果:

{tagId = 1, isTagSelected = true, tagName = "a"}
{tagId = 2, isTagSelected = false, tagName = "b"}
{tagId = 3, isTagSelected = true, tagName = "c"}
我想要的是,如果标记名
a
,则添加
Size=“S”
else
string.empty
null
。我怎样才能做到这一点?我尝试了下面的方法,即在所有的地方添加大小,然后从不需要的地方移除,但它不起作用

代码:

结果:

{tagId = 1, isTagSelected = true, tagName = "a", Size = "S"}
{tagId = 2, isTagSelected = false, tagName = "b", Size = "S"}
{tagId = 3, isTagSelected = true, tagName = "c", Size = "S"}
我尝试了
foreach
循环,但我可以更改只读属性。我是否可以从“b”和“c”中删除
Size

预期结果:

{tagId = 1, isTagSelected = true, tagName = "a", Size = "S"}
{tagId = 2, isTagSelected = false, tagName = "b", Size = "" or null}
{tagId = 3, isTagSelected = true, tagName = "c", Size = "" or null}

我不是在一个VS的实例,但似乎这将工作

var tagDetails = tagIdList.Select((x, i) => new 
        { tagId = x,
          isTagSelected = tagSelectionList[i], 
          tagName = tagList[i], 
          Size = tagList[i] == "a" ? "S" : null 
    }).ToList();

匿名类型的所有属性都是只读的。您必须找到一种方法来生成值已经正确的实例。这可能意味着第二次查询,您将在其中生成一组新的值。这就是我要查找的。非常感谢。
{tagId = 1, isTagSelected = true, tagName = "a", Size = "S"}
{tagId = 2, isTagSelected = false, tagName = "b", Size = "" or null}
{tagId = 3, isTagSelected = true, tagName = "c", Size = "" or null}
var tagDetails = tagIdList.Select((x, i) => new 
        { tagId = x,
          isTagSelected = tagSelectionList[i], 
          tagName = tagList[i], 
          Size = tagList[i] == "a" ? "S" : null 
    }).ToList();