我想将参数传递给 linq 查询...
public IEnumerable GetPhotos()
{
PhotoDBDataContext db = new PhotoDBDataContext();
var query = from p in db.Photos
orderby p.PhotoId descending
select new { p.Album, p.AlbumId, p.Description, p.Photographer,
p.PhotographerId, p.PhotoId, p.Tags, p.Thumbnail,
p.Url };
return query;
}
在上面的例子中使用了“orderby p.PhotoId descending”,我想用参数代替p.PhotoId
有没有可能...
最佳答案
public IQueryable<Photo> GetPhotos(PhotoDBDataContext db, string orderBy)
{
var query = from p in db.Photos select p;
switch (orderBy) {
case "PhotoId":
return query.OrderBy(p => p.PhotoId);
case "AlbumId":
return query.OrderBy(p => p.AlbumId);
default:
// Error handling.
}
}
请注意,您不应返回匿名类型的对象。
关于linq-to-sql - 我如何将参数传递给linq查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2749428/