如何通过特定字段(不是主键)获取实体?

问题描述 投票:0回答:2

我已成功通过Id(主键)获取数据。但是,如果我通过另一个领域调用Get,为什么总是使用Id

这是我的代码:

Aitempatapasurvinsis

public interface ITempatAppService:IApplicationService
{
    GetTempatOutput GetTempatById(GetTempatInput input);

    GetTempatOutput GetTempatByIdKategori(GetTempatKategori input);
}

GetTempatInput.cs

public class GetTempatInput
{
    public int Id { get; set; }
}

GetTempatOutput.cs

public class GetTempatKategori
{
    public int IdKategori { get; set; }
}

TempatAppService.cs

public class TempatAppService:ApplicationService,ITempatAppService
{
    private readonly ITempatManager _tempatManager;
    public TempatAppService(ITempatManager tempatManager)
    {
        _tempatManager = tempatManager;
    }

    public GetTempatOutput GetTempatById(GetTempatInput input)
    {
        var getTempat = _tempatManager.GetTempatById(input.Id);
        GetTempatOutput output = Mapper.Map<MasterTempat, GetTempatOutput>(getTempat);
        return output;
    }

    public GetTempatOutput GetTempatByIdKategori(GetTempatKategori input)
    {
        var getTempat = _tempatManager.GetTempatByIdKategori(input.IdKategori);
        GetTempatOutput output = Mapper.Map<MasterTempat, GetTempatOutput>(getTempat);
        return output;
    }
}

这是我的TempatManager.cs:

public class TempatManager : DomainService, ITempatManager
{
    private readonly IRepository<MasterTempat> _repositoryTempat;
    public TempatManager(IRepository<MasterTempat> repositoryTempat)
    {
        _repositoryTempat = repositoryTempat;
    }

    public MasterTempat GetTempatById(int Id)
    {
        return _repositoryTempat.Get(Id);
    }

    public MasterTempat GetTempatByIdKategori(int IdKategori)
    {
        return _repositoryTempat.Get(IdKategori);
    }
}
c# asp.net-core repository aspnetboilerplate asp.net-boilerplate
2个回答
0
投票

命名参数IdKategori不会使其按该列进行搜索。做这个:

public MasterTempat GetTempatByIdKategori(int IdKategori)
{
    return _repositoryTempat.GetAll().First(t => t.IdKategori == IdKategori);
}

0
投票

获取所选kategori的列表。

public List<MasterTempat> GetTempatByIdKategori(int IdKategori)
{
    return _repositoryTempat.GetAll().Where(t => t.IdKategori == IdKategori).ToList();
}
© www.soinside.com 2019 - 2024. All rights reserved.