2015-07-28 69 views
2

我有一个类LINQ到填充类

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; } 
} 

我可以使用以下的方法手动填充的值:

编辑: //列和数据是样品字节[]值

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(); 

在值获得一个错误,如:

类型的表达式“字符串[]”没有在随后允许从子句的IEnumerable <>

+0

你从哪里得到column1和data1?我们如何从'valuesset'中提取它们? –

回答

2

你会做这样的事情:

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(); 

现在,由于你的代码不清楚它应该如何正确工作,所以答案也不是那么完美。但是这应该给你基本的想法。

+0

再说一次:当我使用select create(byte1 [],new [] {new Cell {column = Encoding.UTF8.GetBytes(column1),data = Encoding.UTF8.GetBytes(l [0] .ToString() )},我得到一个异常“运行时无法评估l [0]”和执行后出现“超出范围异常”。是否无法在select create(// here)中访问“l”值? – user1907849

+1

@ user1907849 - 你真的不清楚你的类型是什么,但'l'看起来像是一个'string',所以'l [0]'是一个'char'。这就是你所期望的吗? – Enigmativity