Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List' [closed]

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<ambiguous object code,name>' to 'System.Collections.Generic.List<intermediary>' [closed]



I am doing a query with IEnumerable and it seems I don't understand why I'm wrong.
I want to return certain columns from my table. But something is wrong.


public class Intermediary

public string CODE get; set;
public string Name get; set;


public async Task<IEnumerable<Intermediary>> GetTest(string company)

var db = new SibaCiidDbContext();
var results = (from o in db.Intermediary
where o.CompanyCode == company
select o).Select(o => new

CODE = o.CompanyCode,
NAME = o.FullName
);

return await Task.FromResult(results.ToList());



This question appears to be off-topic. The users who voted to close gave this specific reason:





Something is wrong, but we cannot guess what, you need to tell us what doesn't work. Also, what do you expect await Task.FromResult(result) to do?
– Camilo Terevinto
Aug 10 at 17:33


await Task.FromResult(result)





I think the problem is that you aren't returning IEnumerable<Intermediary> you are returning IEnumerable<object> because you are creating an anonymous type in your query and an anonymous type can only be cast to object.
– Kevin
Aug 10 at 17:36






I downvoted because your problem statement is not helpful.
– EJoshuaS
Aug 10 at 17:47





Maybe Select(o => new Intermediary CODE = o.CompanyCode, NAME = o.FullName helps. (I added the type name Intermediary)
– Olivier Jacot-Descombes
Aug 10 at 17:48


Select(o => new Intermediary CODE = o.CompanyCode, NAME = o.FullName


Intermediary





@OlivierJacot-Descombes is close, it would be... new Intermediary CompanyCode = o.CompanyCode, FullName = o.FullName
– Kevin
Aug 10 at 17:52




2 Answers
2



Entity Framework has some async methods. You should use ToListAsync() but your method has to return the complete object. When you receive the object returned by the method, then you can use the Select method to get what you want.


public async Task<IEnumerable<Intermediary>> GetTest(string company)


var db = new SibaCiidDbContext();
var results = (from o in db.Intermediary where o.CompanyCode == company select o);

return await results.ToListAsync();



So after this you can use it like this


List<Intermediary> objectList = await object.GetTest(nameCompany);
var anotherList = objectList.Select(o => new
CODE = o.CompanyCode,
NAME = o.FullName
);



if you want to return just these properties, then you might create an object with these properties. For example:


public class SomeObject

public string CODE get; set;
public string Name get; set;


public async Task<IEnumerable<SomeObject>> GetTest(string company)


var db = new SibaCiidDbContext();
var results = (from o in db.Intermediary where o.CompanyCode == company select new SomeObjectCODE = o.CompanyCode, NAME = o.FullName );

return await results.ToListAsync();



assuming your database table is the same as the class name (Intermediary) and using System.Linq; and
using Microsoft.EntityFrameworkCore;


public async Task<IEnumerable<Intermediary>> GetTest(string company)

using (var context = new SibaCiidDbContext())

return
await context.Intermediary
.Where(query => query.CompanyCode == company)
.Select(result =>
new Intermediary

CODE = result.CompanyCode,
NAME = result.FullName
)
.ToListAsync();




Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)