LINQ:查询主体必须以select子句或group子句结束

时间:2015-06-15 11:38:50

标签: c# linq

当我执行我的LINQ代码时,我收到错误查询正文必须以select子句或group子句结束

我很少使用LINQ,这就是无法弄清代码问题的原因。所以任何人都可以看到我的LINQ代码,并指导我错误的错误。

    string[] csvlines = File.ReadAllLines(filename);
    var query = from csvline in csvlines
    let data = csvline.Split(',').Skip(1)
    .Select(a => new User
    {
        CSRName = data[6],
        CallStart = data[0],
        CallDuration = data[1],
        RingDuration = int.Parse(data[2]),
        Direction = int.Parse(data[3]),
        IsInternal = int.Parse(data[4]),
        Continuation = int.Parse(data[5]),
        ParkTime = int.Parse(data[7])
    })
    .ToList();

2 个答案:

答案 0 :(得分:3)

此:

var query = from csvline in csvlines
let data = csvline.Split(',').Skip(1)

无效的LINQ语法。它应该是:

string csvlines = File.ReadAllText(filename);
var query = csvlines.Split(',')
                    .Skip(1)
                    .Select(data => new User
{
    CSRName = data[6],
    CallStart = data[0],
    CallDuration = data[1],
    RingDuration = int.Parse(data[2]),
    Direction = int.Parse(data[3]),
    IsInternal = int.Parse(data[4]),
    Continuation = int.Parse(data[5]),
    ParkTime = int.Parse(data[7])
}).ToList();

答案 1 :(得分:0)

我认为最顶行是标题行。您在这里混合查询和方法语法。您可以编写查询,如下所示:

var query = csvlines.Skip(1).Select(x =>
        {
            var data = x.Split(',');

            return new User
            {
                CSRName = data[6],
                CallStart = data[0],
                CallDuration = data[1],
                RingDuration = int.Parse(data[2]),
                Direction = int.Parse(data[3]),
                IsInternal = int.Parse(data[4]),
                Continuation = int.Parse(data[5]),
                ParkTime = int.Parse(data[7])
            };
        });