Linq 填充 class 的值
Linq to populate values of a class
我有一个class
public class Row : IExtensible
{
public Row();
[ProtoMember(1, IsRequired = true, Name = "key", DataFormat = DataFormat.Default)]
public byte[] key { get; set; }
[ProtoMember(2, Name = "values", DataFormat = DataFormat.Default)]
public List<Cell> values { get; }
}
我可以使用以下方法手动填充:
编辑:
//列和数据是样本byte[] values
CellSet.Row row = new CellSet.Row { key = sampleKey };
Cell value = new Cell { column = column1, data = data1 };
row.values.Add(value);
我需要在 LINQ 中填充这些值,这是我一直在尝试的:
var result =
(
from a in firstrow
let valuesset = a.Split(',')
from l in valuesset
select new CellSet.Row
{
key = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()),
//values = new List<Cell>() //Not possible since only get is there
}
).ToList();
如何给对象添加值CellSet.Row?
这个我也试过
//Edit:
//Read the xml file row by row and process it
var result =
(
from a in firstrow
let valuesset = a.Split(',')
from l in valuesset
select new CellSet.Row
{
key = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()),
//values = new List<Cell>() //Not possible since only get is there
}.values.Add(value12)
).ToList();
在值中出现错误,例如:
An expression of type 'string[]' is not allowed in a subsequent from clause with IEnumerable<>
你会这样做:
Func<byte[], IEnumerable<Cell>, Row> create =
(k, cs) =>
{
CellSet.Row row = new CellSet.Row { key = k };
row.values.AddRange(cs);
return row;
};
var result =
(
from a in firstrow
let valuesset = a.Split(',')
from l in valuesset
select create(
Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()),
new [] { value12 })
).ToList();
现在,由于您的代码不清楚它究竟应该如何工作,因此答案也不完全存在。但这应该给你基本的想法。
我有一个class
public class Row : IExtensible
{
public Row();
[ProtoMember(1, IsRequired = true, Name = "key", DataFormat = DataFormat.Default)]
public byte[] key { get; set; }
[ProtoMember(2, Name = "values", DataFormat = DataFormat.Default)]
public List<Cell> values { get; }
}
我可以使用以下方法手动填充:
编辑: //列和数据是样本byte[] values
CellSet.Row row = new CellSet.Row { key = sampleKey };
Cell value = new Cell { column = column1, data = data1 };
row.values.Add(value);
我需要在 LINQ 中填充这些值,这是我一直在尝试的:
var result =
(
from a in firstrow
let valuesset = a.Split(',')
from l in valuesset
select new CellSet.Row
{
key = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()),
//values = new List<Cell>() //Not possible since only get is there
}
).ToList();
如何给对象添加值CellSet.Row?
这个我也试过
//Edit:
//Read the xml file row by row and process it
var result =
(
from a in firstrow
let valuesset = a.Split(',')
from l in valuesset
select new CellSet.Row
{
key = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()),
//values = new List<Cell>() //Not possible since only get is there
}.values.Add(value12)
).ToList();
在值中出现错误,例如:
An expression of type 'string[]' is not allowed in a subsequent from clause with IEnumerable<>
你会这样做:
Func<byte[], IEnumerable<Cell>, Row> create =
(k, cs) =>
{
CellSet.Row row = new CellSet.Row { key = k };
row.values.AddRange(cs);
return row;
};
var result =
(
from a in firstrow
let valuesset = a.Split(',')
from l in valuesset
select create(
Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()),
new [] { value12 })
).ToList();
现在,由于您的代码不清楚它究竟应该如何工作,因此答案也不完全存在。但这应该给你基本的想法。