我正在尝试实现搜索功能,但我被卡住了

时间:2016-05-28 17:59:07

标签: c# asp.net-mvc database single-page-application

这些是我的歌曲和播放列表模型:

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表达式无法转换为表达式树

1 个答案:

答案 0 :(得分:0)

只需使用Playlist导航属性:

return View(songset.Where(x => x.Playlist.PlaylistName.Contains(search)).ToList());