C# 无法在内部联接上隐式转换类型system.linq.iqueryable
谈到asp.net,我是个新手,但我正试图在asp.net中创建一个restfull api。在下面的代码中,我试图从Saldo和Vereniging获取数据 saldo表包含: -萨尔多伊德 -维尼金吉德 -利迪德 -抹布 Vereniging表包含: 维尼金吉德 纳姆 地点 facebookgroupId 我试图创建的是return语句返回1 LidId中的所有VerenigingenC# 无法在内部联接上隐式转换类型system.linq.iqueryable,c#,asp.net,entity-framework,linq,C#,Asp.net,Entity Framework,Linq,谈到asp.net,我是个新手,但我正试图在asp.net中创建一个restfull api。在下面的代码中,我试图从Saldo和Vereniging获取数据 saldo表包含: -萨尔多伊德 -维尼金吉德 -利迪德 -抹布 Vereniging表包含: 维尼金吉德 纳姆 地点 facebookgroupId 我试图创建的是return语句返回1 LidId中的所有Verenigingen public class LibraryRepository : ILibraryRepository {
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);
}
公共类LibraryRepository:ILibraryRepository
{
私人图书馆环境(u context);;
公共LibraryRepository(LibraryContext上下文)
{
_上下文=上下文;
}
公共bool Save()
{
返回(_context.SaveChanges()>=0);
}
公共IEnumerable GetVerenigingenperLid(int lidId)
{
return _context.Vereniging//“from”语句中的起始点-表
.Join(_context.Saldo,//内部联接的源表
a=>a.verenigingId,//选择主键(sql“join”语句中“on”子句的第一部分)
b=>b.verenigingId,//选择外键(“on”子句的第二部分)
(a,b)=>new{Vereniging=a,Saldo=b})//选择
其中(c=>c.Saldo.lidId==lidId);
}
}
我得到一个错误:
无法隐式转换类型“system.linq.IQueryable”您的返回类型应与投影中的类型匹配。请参见下图
在查询末尾添加一个
Select(…)
:
// ...
.Where(c => c.Saldo.lidId == lidId)
// for example
.Select(c => c.Vereniging);
您的返回类型应与投影中的类型匹配。请参见下图
在查询末尾添加一个
Select(…)
:
// ...
.Where(c => c.Saldo.lidId == lidId)
// for example
.Select(c => c.Vereniging);
当您在选择中返回
(a,b)=>new{Vereniging=a,Saldo=b}
时,getverenigenperlid的返回类型为IEnumerable
要解决这个问题,您需要声明新的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);
}
公共类视图模型
{
公共验证{get;set;}
公共Saldo{get;set;}
}
公共IEnumerable GetVerenigingenperLid(int lidId)
{
return _context.Vereniging//“from”语句中的起始点-表
.Join(_context.Saldo,//内部联接的源表
a=>a.verenigingId,//选择主键(sql“join”语句中“on”子句的第一部分)
b=>b.verenigingId,//选择外键(“on”子句的第二部分)
(a,b)=>newviewmodel{Vereniging=a,Saldo=b})//选择
其中(c=>c.Saldo.lidId==lidId);
}
希望它有帮助当您在选择中返回新的{Vereniging=a,Saldo=b}时,getverenigenperlid的返回类型是
IEnumerable
要解决这个问题,您需要声明新的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);
}
公共类视图模型
{
公共验证{get;set;}
公共Saldo{get;set;}
}
公共IEnumerable GetVerenigingenperLid(int lidId)
{
return _context.Vereniging//“from”语句中的起始点-表
.Join(_context.Saldo,//内部联接的源表
a=>a.verenigingId,//选择主键(sql“join”语句中“on”子句的第一部分)
b=>b.verenigingId,//选择外键(“on”子句的第二部分)
(a,b)=>newviewmodel{Vereniging=a,Saldo=b})//选择
其中(c=>c.Saldo.lidId==lidId);
}
希望能有帮助