将 Int 数组传递给 LINQ 运算符以获取特定对象
pass an Array of Int to LINQ Operators for Getting particular objects
我有一个 int 数组,这个数组有对象的 ID,我想把这个数组传递给 LINQ 运算符,并获取所有具有与数组成员相同的 ID 的对象。
我不想使用 for 循环 for 获取这些对象,我只是想知道并学习如何使用数组获取这些对象联机
提前致谢
public IEnumerable<ExhibitionPhoto> GetPhotosById(int[] ids)
{
return _db.ExhibitionPhotos.Select();
}
型号:
public class ExhibitionPhoto
{
[Key]
public int ExhibitionPhotoId { get; set; }
public int ExhibitionId { get; set; }
public string PhotoPath { get; set; }
#region Navigational Properties
[ForeignKey(nameof(ExhibitionId))]
public Exhibition Exhibition { get; set; }
}
您可以使用Where()
clause with .Any()
public IEnumerable<ExhibitionPhot> GetPhotosById(int[] ids)
{
return _db.ExhibitionPhotos.Where(x => ids.Any(y => y == x.ExhibitionPhotoId));
}
Where()
:
Filters a sequence of values based on a predicate.
Any()
:
Determines whether any element of a sequence exists or satisfies a condition.
我有一个 int 数组,这个数组有对象的 ID,我想把这个数组传递给 LINQ 运算符,并获取所有具有与数组成员相同的 ID 的对象。 我不想使用 for 循环 for 获取这些对象,我只是想知道并学习如何使用数组获取这些对象联机 提前致谢
public IEnumerable<ExhibitionPhoto> GetPhotosById(int[] ids)
{
return _db.ExhibitionPhotos.Select();
}
型号:
public class ExhibitionPhoto
{
[Key]
public int ExhibitionPhotoId { get; set; }
public int ExhibitionId { get; set; }
public string PhotoPath { get; set; }
#region Navigational Properties
[ForeignKey(nameof(ExhibitionId))]
public Exhibition Exhibition { get; set; }
}
您可以使用Where()
clause with .Any()
public IEnumerable<ExhibitionPhot> GetPhotosById(int[] ids)
{
return _db.ExhibitionPhotos.Where(x => ids.Any(y => y == x.ExhibitionPhotoId));
}
Where()
:
Filters a sequence of values based on a predicate.
Any()
:
Determines whether any element of a sequence exists or satisfies a condition.