简体   繁体   中英

C# implicit conversion error from generic list to Ienumerable

public IEnumerable<XdbActiveDiscipline> GetAllDisciplineDocs(string projectNumber)
{
    try
    {
        // return _MigratorDBContext.XdbActiveDiscipline.Where(x => x.OtProjectNumber == projectNumber && x.IsProcessed==null).ToList();
        var val = _MigratorDBContext.XdbActiveDiscipline.Where(x => x.OtProjectNumber == projectNumber && x.IsProcessed == null).OrderBy(x => x.DocumentReference).GroupBy(x => new { x.DocumentReference, x.DocumentRevisionNumber, x.DocumentRevisionObject })
            .Select(g => new { g, count = g.Count() })
            .SelectMany(t => t.g.Select(b => b)
                .Zip(Enumerable.Range(1, t.count), (j, i) => new
                {
                    j.OtProjectNumber,
                    rn = i
                }))
            .ToList();

        return val;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return null;
    }
}

return val: Throws the error

And the code is written to add a rownumber() value at the last of the result set (ie partition by)

在此处输入图像描述

Your return value is not typeof(XdbActiveDiscipline) . As you can see the error says missing a cast? and the method expects an IEnumerable of XdbActiveDiscipline in return.

The element you are selecting on the linq query is an anonymous type different from the expected return, you can see that if you hover over the val when instantiating.

Val is List of anonymos, not List < XdbActiveDiscipline >. Change return of the action to this:

return val.Select( i=> new XdbActiveDiscipline{
Id=i.Id,
.... and so on
}).ToArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM