2015-07-28 112 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