我的代码应该检查两个条件并返回值,但是当我尝试返回q时时,会出现此错误
无法将类型“System.Collections.Generic.List<<匿名类型:字符串名称,字符串文件>>”隐式转换为“System.Collections.Generic.List<字符串>”
我尝试了一切,但没有任何效果,也不知道如何设置List<string>其设置为List<EF_Model.PDF>,PDF 是我模型中的 DTO
这是我的代码
internal List<string> Customers_File(int _id)
{
using (var Context = new EF_Model.CoolerEntities())
{
var q = from c in Context.Customers
where c.Id == _id &&
c.Ref_PDF != null
select new { c.PDF.Name, c.PDF.File };
return q.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
您必须将匿名对象转换为字符串表示形式。(注意我使用的是 C# 6.0 功能 - 字符串插值,您可以将其替换为以前版本中的 string.Format。示例:
return q.Select(x=>$"Name = {x.PDF.Name} File = {c.PDF.File}").ToList();
Run Code Online (Sandbox Code Playgroud)