lou*_*wen 5 c# mysql linq sql-server lambda
我得到了这个我想要转换为Linq的SQL查询.这是一个问题:
我正在制作一个需要从3个不同的表中返回值的asp.net api
CREATE TABLE Locatie (
locatieId INT IDENTITY(1,1) not null,
postcode VARCHAR(10) not null,
huisnummer INT not null,
adres VARCHAR(50) not null,
plaats VARCHAR(50) not null,
CREATE TABLE Vereniging (
verenigingId INT IDENTITY(1,1) not null,
locatieId INT not null,
naam VARCHAR(50) not null,
facebookGroupId BIGINT null,
CREATE TABLE Saldo (
saldoId INT IDENTITY(1,1) not null,
lidId INT not null,
verenigingId INT not null,
bedrag SMALLMONEY not null,
Run Code Online (Sandbox Code Playgroud)
我遗漏了所有的外国钥匙和小学生.这只是为了澄清我想要的东西.我的问题是,我有一个需要从几个表返回信息的函数.sql查询看起来像这样=
Select v.verenigingId, l.postcode, l.huisnummer, l.adres,l.plaats,v.naam,v.facebookGroupId
from Vereniging v inner join Saldo s
on v.verenigingId = s.verenigingId
inner join Locatie l
on v.locatieId=l.locatieId
where s.lidId = 1;
Run Code Online (Sandbox Code Playgroud)
我从lidid = 1获得所有"verenigingen"并显示"verenigingen"在表位置中的所有信息.
但是当我尝试使用linq/lambda这样做时,它会出错; 我的函数看起来像这样:
public class LibraryRepository : ILibraryRepository
{
private LibraryContext _context;
public LibraryRepository(LibraryContext context)
{
_context = context;
}
public bool Save()
{
return (_context.SaveChanges() >= 0);
}
public IEnumerable<Verenigingmodel> GetVerenigingenperLid(int lidId)
{
return _context.Vereniging
.Join(
_context.Saldo.Where(b => b.lidId == lidId),
ver => ver.verenigingId,
sal => sal.verenigingId,
(ver, sal) => new Viewmodel { Vereniging = ver, Saldo = sal })
.Join(
_context.Locatie,
verr => verr.Vereniging.locatieId,
loca => loca.locatieId,
(vr, loca) => new Viewmodel { Locatie = loca });
//this returns wrong sql information
}
Run Code Online (Sandbox Code Playgroud)
}
我的verenigingmodel看起来像这样:
public class Verenigingmodel
{
public int verenigingId { get; set; }
public string postcode { get; set; }
public int huisnummer { get; set; }
public string adres { get; set; }
public string plaats { get; set; }
public string naam { get; set; }
public int facebookGroupId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的库上下文如下所示:
public class LibraryContext : DbContext
{
public LibraryContext(DbContextOptions<LibraryContext> options)
: base(options)
{
Database.Migrate();
}
public DbSet<Gebruiker> Gebruiker { get; set; }
public DbSet<Lid> Lid { get; set; }
public DbSet<Vereniging> Vereniging { get; set; }
public DbSet<Saldo> Saldo { get; set; }
public DbSet<Locatie> Locatie { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图实现的是,我将所有不同的信息放在vereniging模型中,并从那里把它作为我的休息api的输出:
[HttpGet("api/Vereniging/{lidId}")]
public IActionResult FindVereniGingenPerLid(int lidId)
{
var verenigingFromRepo = vlibraryRepository.GetVerenigingenperLid(lidId);
return new JsonResult(verenigingFromRepo);
}
Run Code Online (Sandbox Code Playgroud)
我会做一些不同的功能。像这样:
public IEnumerable<Verenigingmodel> GetVerenigingenperLid(int lidId)
{
return (
from v in _context.Vereniging
join s in _context.Saldo
on v.verenigingId equals s.verenigingId
join l in _context.Locatie
on v.locatieId equals l.locatieId
select new Verenigingmodel()
{
verenigingId= v.verenigingId,
postcode=l.postcode,
huisnummer=l.huisnummer,
adres=l.adres,
naam=v.naam,
facebookGroupId=v.facebookGroupId,
plaats=l.plaats
}
).ToList();
}
Run Code Online (Sandbox Code Playgroud)
我个人发现更容易看到这样的连接并将结果组合到一个对象中
| 归档时间: |
|
| 查看次数: |
263 次 |
| 最近记录: |