c# and Linq query to object[,]

时间:2017-12-18 07:10:54

标签: c# excel linq

I have a list of query results, each containing objects different from the others. I have one type per data set.

var myQueryresult = _myContext.TableA
    .Where(a => a.IsToBeProcessed)
    .Select(x => new { ColumnA = x.FieldA, ColumnB =  x.FieldB })
    .ToList();

The end goal is to store this data in an Excel sheet using the

Range firstCell = sheet.Cells[1, 1];
Range lastCell = sheet.Cells[data.GetLength(0) + 1, data.GetLength(1)];
sheet.Range[firstCell, lastCell].Value2 = myObjectArrayofObjectArrays;

I can't seem to find any way to do this but looping on each row of the result.

2 个答案:

答案 0 :(得分:0)

You can use the Excel library's NamedRange.get_Resize Method as well as NamedRange.set_Value Method (Object, Object) as follows:

    <issuetracker>
    <apikey>3fe68920f3cf</apikey>
    <uribase>https://ServerIP/redmine</uribase>
    <uriview>https://ServerIP/redmine/issues/</uriview>
    <projectidentifier>CM</projectidentifier>
    </issuetracker>

EDIT

I misunderstood the requirements. For your first part it does seem like a loop going through each element is the best method.

答案 1 :(得分:0)

您可以通过json将数据集转换为数据表

var json = JsonConvert.SerializeObject(results);
var table = (DataTable)JsonConvert.DeserializeObject(json, typeof(DataTable))

然后依靠您选择的引擎将数据表输出为excel ready格式