[英]cannot implicitly convert type system.linq.iqueryable on inner join
im a rookie when it comes to asp.net but im trying to create a restfull api in asp.net. 我是一个新手,当谈到asp.net时,我却试图在asp.net中创建一个restfull api。 in the code below im trying to get data from Saldo and Vereniging. 在下面的代码中,im试图从Saldo和Vereniging获取数据。
the saldo table contains: saldo表包含:
-saldoId -VerenigingId -LidId -Bedrag -saldoId -VerenigingId -LidId -Bedrag
the Vereniging table contains: Vereniging表包含:
VerenigingId naam locatie facebookgroupId VerenigingId naam locatie facebookgroupId
what im trying to create is that the return statement returns all the Verenigingen from 1 LidId. 我试图创建的是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);
}
} }
i get the error: Cannot implicitly conver type 'system.linq.IQueryable "<<"anonymous type: Library.API.Entities.Vereniging Vereniging, Library.API.Entities.Saldo Saldo>> to 'System.Collections.Generic.IEnumerable "<"Library.API.Entities.Vereniging>. 我收到错误:无法隐式将类型'system.linq.IQueryable“ <<”匿名类型转换为Library.API.Entities.Vereniging Vereniging,Library.API.Entities.Saldo Saldo >>到'System.Collections.Generic.IEnumerable “ <” Library.API.Entities.Vereniging>。 An explicit conversion exists (are u missing a cast?). 存在显式转换(您是否缺少演员表?)。
I understand the problem im having. 我了解我遇到的问题。 its because the return value consists of a piece Saldo and a Piece Vereniging. 这是因为返回值包含一个Saldo和一个Vereniging。
but i dont know how to fix this or avoid it. 但我不知道如何解决或避免它。
Return type of GetVerenigingenperLid is IEnumerable<Vereniging>
, while you are returning (a, b) => new { Vereniging = a, Saldo = b }
in the select. GetVerenigingenperLid的返回类型为IEnumerable<Vereniging>
,而您在选择中返回(a, b) => new { Vereniging = a, Saldo = b }
时。
To fix this, you need to declare new viewmodel class say Viewmodel which will contain Vereniging and Saldo as properties. 要解决此问题,您需要声明一个新的viewmodel类,即Viewmodel ,它将包含Vereniging和Saldo作为属性。
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);
}
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.