gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:03:52 25 4
gpt4 key购买 nike

我是 C# 和 Linq 的新手。

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

//错误:

System.InvalidCastException: Specified cast is not valid..

//编辑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)
{
query = query.Where(x => x.audit.RecordId == Id.ToString());
}
if (fromTime.HasValue)
{
var tmp = new DateTimeOffset(fromTime.Value.ToUniversalTime());
query = query.Where(x => x.audit.EventDateUTC >= tmp);
}
if (toTime.HasValue)
{
var tmp = new DateTimeOffset(toTime.Value.ToUniversalTime());
query = query.Where(x => x.audit.EventDateUTC <= tmp);
}
if (!string.IsNullOrEmpty(type))
{
var parseEvent = (EventType)Enum.Parse(typeof(EventType), type);
query = query.Where(x => x.audit.EventType == parseEvent);
}
if (!string.IsNullOrEmpty(user))
{
query = query.Where(x => x.audit.UserName == user);
}
if (pager != null)
{
var totalRecords = query.Count();
pager.TotalRecords = totalRecords;
var data = query.Select(x =>
new AuditInfo
{
x.audit.TypeFullName, //Here Error Occurs
x.audit.UserName,//Here Error Occurs
x.audit.EventType,//Here Error Occurs
x.audit.EventDateUTC,//Here Error Occurs
@LogDetails = x.audit.LogDetails.ToList(), //Here Error Occurs
x.entaudits.Name,
@Description = x.entaudits.Description
})
.OrderByDescending(x => x.EventDateUTC)
.Skip(pager.From)
.Take(pager.PageSize);
try
{
var list1 = data.ToList<AuditInfo>();
}
catch (Exception e)
{
}
var list = data.ToList<AuditInfo>();
pager.RecordCount = list.Count;
return list;
}
else
{
var list = query.Select(x =>
new AuditInfo
{
x.audit.TypeFullName,
x.audit.UserName,
x.audit.EventType,
x.audit.EventDateUTC,
@LogDetails = x.audit.LogDetails.ToList(),
x.entaudits.Name,
@Description = x.entaudits.Description
})
.OrderByDescending(x => x.EventDateUTC)
.ToList<AuditInfo>();
return list;
}
}
}

当我调试代码 totalRecords 变量显示计数 6,但显示异常消息 Specified cast is not valid at this line var list1 = data.ToList();

最佳答案

您必须将匿名对象转换为动态对象。

要使用 linq 执行此操作,您可以使用 Cast<dynamic>林克方法:

var list = query.Select(x =>
new
{
x.audit.TypeFullName,
x.audit.UserName,
x.audit.EventType,
x.audit.EventDateUTC,
@LogDetails = x.audit.LogDetails.ToList(),
x.entaudits.Name,
@Description = x.entaudits.Description
})
.OrderByDescending(x => x.EventDateUTC)
.AsEnumerable()
.Cast<dynamic>()
.ToList<dynamic>(); \\ here exception occures
return list;

关于c# - 如何在 C# 中使用 Linq to SQL 将匿名类型转换为 List <dynamic>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53060869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com