[英].NET C# I want to get the IDs (1/multiple) from a data if it much the ID of another table
Models:楷模:
namespace practise_API.Model
{
public class SourceAttributes
{
[Key]
public int SourceEntityId { get; set; }
public int ATiD { get; set; }
public string Name { get; set; }
public string Datatype { get; set; }
}
public class OtherData
{
public int ID { get; set; }
public string Ename { get; set; }
public string Type { get; set; }
public string Source { get; set; }
public string Frequency { get; set; }
}
}
IRepository
interface: IRepository
接口:
namespace API.Repository
{
public interface ISourceRepository
{
IEnumerable<SourceEntities> GetSourceEntities();
IEnumerable<SourceAttributes> GetSourceAttributes(int id);
........
void Save();
}
}
Repository
implementation: Repository
实现:
public class SourceRepository : ISourceRepository
{
private readonly MapperDbContext _dbContext;
public SourceRepository(MapperDbContext dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<SourceAttributes> GetSourceAttributes()
{
return _dbContext.SourceAttributes
.Where(i => i.SourceEntityId == OtherData.ID)
.ToList();
}
}
I want to get all the SourceAttributes.SourceentityIds
that match that one ID from OtherData
.我想从
OtherData
SourceAttributes.SourceentityIds
The way I thought about it was to use where() System.Linq but I can't get the OtherData.ID.我想到的方法是使用 where() System.Linq 但我无法获得 OtherData.ID。
Also this is going to be called later in my Get()
of my controller.这也将在我的 controller 的
Get()
中调用。
The ISourceRepository.GetSourceAttributes
has an id
parameter, but the implementation is missing it resulting surely in a compiler error. ISourceRepository.GetSourceAttributes
有一个id
参数,但实现缺少它肯定会导致编译器错误。 Modify it:修改它:
public IEnumerable<SourceAttributes> GetSourceAttributes(int otherDataId)
{
return _dbContext.SourceAttributes
.Where(i => i.SourceEntityId == otherDataId)
.ToList();
}
To get the ids you want:要获取您想要的 ID:
var ids = sourceRepository
.GetSourceAttributes(myOtherDataInstance.ID)
.Select(i => i.ID)
.ToArray(); // or leave it as IEnumerable as you wish
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.