这些是我的歌曲和播放列表模型:
namespace Konacno.Models
{
using System;
using System.Collections.Generic;
public partial class Song
{
public int Id { get; set; }
public string Name { get; set; }
public string Singer { get; set; }
public string Year { get; set; }
public int PlaylistId { get; set; }
public virtual Playlist Playlist { get; set; }
}
}
namespace Konacno.Models
{
using System;
using System.Collections.Generic;
public partial class Playlist
{
public Playlist()
{
this.Song = new HashSet<Song>();
}
public int Id { get; set; }
public string PlaylistName { get; set; }
public virtual ICollection<Song> Song { get; set; }
}
}
这是我的SongController里面的内容:
public ActionResult Index(string searchBy, string search)
{
var songset = db.SongSet.Include(s => s.Playlist);
if (searchBy == "Name")
{
return View(songset.Where(x => x.Name.Contains(search)).ToList());
}
else if (searchBy == "Singer")
{
return View(songset.Where(x => x.Singer.Contains(search)).ToList());
}
else if (searchBy == "Year")
{
return View(songset.Where(x => x.Year.Contains(search)).ToList());
}
else if (searchBy == "Playlist")
{
return View( What should i type here );
}
else {
return View(songset.ToList());
}
}
这就是我的应用的样子: https://imgur.com/1l9BVOE
问题是 - 我应该在Retun中查看内部控制器以查看所选歌曲集是否包含想要的播放列表?
else if (searchBy == "Playlist")
{
return View( songset.Any(x => x.GetType().GetProperties().Any(p =>
{
var value = p.GetValue(x);
return value != null && value.ToString().Contains(search);
}
) ) );
}
但是它说(p为秒。任何带下划线的红色)带有语句体的Lambda表达式无法转换为表达式树
答案 0 :(得分:0)
只需使用Playlist
导航属性:
return View(songset.Where(x => x.Playlist.PlaylistName.Contains(search)).ToList());