0

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

使用 id(主键)搜索

使用 id_kategori(not_primary_key

这是我的代码:

ITempatAppService.cs

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);
    }
}
4

2 回答 2

0

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

public MasterTempat GetTempatByIdKategori(int IdKategori)
{
    return _repositoryTempat.GetAll().First(t => t.IdKategori == IdKategori);
}
于 2018-03-01T14:14:17.657 回答
0

获取所选类别的列表。

public List<MasterTempat> GetTempatByIdKategori(int IdKategori)
{
    return _repositoryTempat.GetAll().Where(t => t.IdKategori == IdKategori).ToList();
}
于 2018-03-02T08:12:06.337 回答