简体   繁体   English

无法在内部联接上隐式转换类型system.linq.iqueryable

[英]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. 但我不知道如何解决或避免它。

Your return type should match the type in projection. 您的退货类型应与投影中的类型匹配。 see illustration below. 参见下图。

不匹配的返回类型
Add a Select(...) to the end of the query: 在查询末尾添加一个Select(...)

   // ...
   .Where(c => c.Saldo.lidId == lidId)
   // for example
   .Select(c => c.Vereniging);

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 ,它将包含VerenigingSaldo作为属性。

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.

相关问题 无法隐式转换类型&#39;System.Linq.IQueryable <AnonymousType#1> &#39;到&#39;System.Linq.IQueryable &lt;&gt;&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<>' 无法将类型'System.Linq.IQueryable <int>'隐式转换为'int?' - Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int?' 无法隐式转换System.Linq.IQueryable类型 <string> 串起来 - Cannot implicitly convert type System.Linq.IQueryable<string> to string 无法隐式转换类型&#39;System.Linq.IQueryable? - Cannot implicitly convert type 'System.Linq.IQueryable? MVC 5无法将类型&#39;System.Linq.IQueryable隐式转换为bool吗? - MVC 5 Cannot implicitly convert type 'System.Linq.IQueryable to bool? 无法隐式转换类型'System.Linq.IQueryable <AnonymousType#1>' - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' 无法隐式转换类型&#39;System.Linq.IQueryable <double> &#39;到&#39;加倍&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<double>' to 'double' 无法隐式转换类型&#39;System.Linq.IQueryable - Cannot implicitly convert type 'System.Linq.IQueryable 无法隐式转换类型&#39;System.Linq.IQueryable <Model> &#39; 模拟&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<Model>' to 'Model' 无法将类型“System.Linq.IQueryable”隐式转换为“System.Data.Entity.DbSet” - Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.DbSet'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM