小编San*_*y N的帖子

如何在 C# 中使用 Linq to SQL 将匿名类型转换为 List <dynamic>

我是 C# 和 Linq 的新手。

实际上,我想将匿名类型返回到列表中。匿名类型包含 List、String 和 DateTime。我尝试使用下面的代码,但它给出了一个错误。请帮助并告诉我我缺少什么或建议我如何实现这一目标。

//错误:

System.InvalidCastException: Specified cast is not valid..
Run Code Online (Sandbox Code Playgroud)

//编辑C#Linq代码

        public List<AuditInfo> GetScanAudit(object Id, DateTime? fromTime, DateTime? toTime, string type = null,
            string user = null, Pager pager = null)
        {
            using (var ctx = new PlantDataContext())
            {
                var query = from audit in ctx.AuditLog
                            join ent in ctx.Scans on audit.RecordId equals ent.Id.ToString() into audits
                            from entaudits in audits.DefaultIfEmpty()
                            where audit.TypeFullName == "ABCD.DB.Model.Scan"
                            select new
                            {
                                audit,
                                entaudits
                            };

                if (Id != null) …
Run Code Online (Sandbox Code Playgroud)

c# linq anonymous-types

3
推荐指数
2
解决办法
3799
查看次数

标签 统计

anonymous-types ×1

c# ×1

linq ×1