Reputation: 10230
my code supposed to check two conditions and return the values but when i try to return q this error shows up
Cannot implicitly convert type 'System.Collections.Generic.List<< anonymous type: string Name, string File>>' to 'System.Collections.Generic.List< string>
and i tried everything but nothing worked also don't know to set List<string>
or set it as List<EF_Model.PDF>
,PDF is a DTO in my model
this is my code
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();
}
}
Upvotes: 5
Views: 13063
Reputation: 136074
You need to define an object with your to properties
public class PdfInfo
{
public string Name{get;set;}
public string File{get;set;}
}
Return a list of them from your method
internal List<PdfInfo> Customers_File(int _id)
And finally project to those, in place of an anonymous object:
....
select new PdfInfo() { Name=c.PDF.Name, File = c.PDF.File };
Upvotes: 1
Reputation: 8921
You are getting this error because you have defined Customers_File
as returning a list of strings. However, the list q
that you return doesn't fit that description.
In your query, when you do
select new { c.PDF.Name, c.PDF.File };
..you are making an Anonymous Type, and storing it in the collection q
. This type has two fields and is clearly not a string
.
Some possible solutions are:
List<object>
instead of List<string>
(not recommended).List<dataClass>
Upvotes: 0
Reputation: 12253
You have declared your return type as string but are returning an a list of anonymous objects with two properties. That won't work. If you want to return a string you need to create a single string per list item. If you want to return object change your return type
Upvotes: 0
Reputation: 6222
You have to convert the anonymous object into a string representation.(Note I'm using C# 6.0 feature - string interpolation, you can replace it with string.Format in the previous versions. Example:
return q.Select(x=>$"Name = {x.PDF.Name} File = {c.PDF.File}").ToList();
Upvotes: 3