简体   繁体   中英

I am trying to implement search functionality but I am stuck

These are my Song and Playlist models:

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; }
    }
}

And this is inside my 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());
            }
        }

This is how my app looks like: https://imgur.com/1l9BVOE


Question is - What should I put in retun View inside controller to check if selected songset contains wanted playlist?

I have tried this:

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);
                    }

                    ) ) );
            }

But it says(p in second .Any underlined red) Lambda expression with a statement body cannot be converted to an expression tree

只需使用“ Playlist导航属性:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM