我是一个新手,当涉及到asp.net但我试图在asp.net中创建一个restfull api。在下面的代码我试图从Saldo和Vereniging获取数据。
saldo表包含:
-saldoId -VerenigingId -LidId -Bedrag
Vereniging表包含:
VerenigingId NAAM locatie facebookgroupId
我试图创建的是return语句从1 LidId返回所有Verenigingen。
public class LibraryRepository : ILibraryRepository
{
private LibraryContext _context;
public LibraryRepository(LibraryContext context)
{
_context = context;
}
public bool Save()
{
return (_context.SaveChanges() >= 0);
}
public IEnumerable<Vereniging> GetVerenigingenperLid(int lidId)
{
return _context.Vereniging // your starting point - table in the "from" statement
.Join(_context.Saldo, // the source table of the inner join
a => a.verenigingId, // Select the primary key (the first part of the "on" clause in an sql "join" statement)
b => b.verenigingId, // Select the foreign key (the second part of the "on" clause)
(a, b) => new { Vereniging = a, Saldo = b }) // selection
.Where(c => c.Saldo.lidId == lidId);
}
}
我收到错误: 无法隐式转换类型&#39; system.linq.IQueryable&#34;&lt;&lt;&#34;匿名类型:Library.API.Entities.Vereniging Vereniging,Library.API.Entities.Saldo Saldo&gt;&gt; to&#39; System.Collections.Generic.IEnumerable&#34;&lt;&#34; Library.API.Entities.Vereniging&gt;。存在显式转换(你是否错过了演员?)。
我理解我遇到的问题。因为返回值包括Saldo和Piece Vereniging。
但我不知道如何解决或避免它。
答案 0 :(得分:2)
您的返回类型应与投影中的类型相匹配。见下图。
// ...
.Where(c => c.Saldo.lidId == lidId)
// for example
.Select(c => c.Vereniging);
答案 1 :(得分:0)
GetVerenigingenperLid 的返回类型为(a, b) => new { Vereniging = a, Saldo = b }
,而您在选择中返回public class ViewModel
{
public Vereniging {get;set;}
public Saldo {get;set;}
}
public IEnumerable<ViewModel> GetVerenigingenperLid(int lidId)
{
return _context.Vereniging // your starting point - table in the "from" statement
.Join(_context.Saldo, // the source table of the inner join
a => a.verenigingId, // Select the primary key (the first part of the "on" clause in an sql "join" statement)
b => b.verenigingId, // Select the foreign key (the second part of the "on" clause)
(a, b) => new ViewModel { Vereniging = a, Saldo = b }) // selection
.Where(c => c.Saldo.lidId == lidId);
}
。
要解决此问题,您需要声明新的viewmodel类说 Viewmodel ,其中包含 Vereniging 和 Saldo 作为属性。
{{1}}
希望有所帮助