0

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://i.stack.imgur.com/fJb70.jpg


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

ivan_r
  • 3
  • 2
  • Do you mean something like [this](http://stackoverflow.com/questions/23527545/linq-query-any-of-the-properties-contains-string) – Odrai May 28 '16 at 18:02
  • While you might be requesting this data using angular, the issue has nothing to do with client side or angular so I have removed that tag. Only use tags that are relevant to the actual problem – charlietfl May 28 '16 at 18:12

1 Answers1

0

Just use the Playlist navigation property:

return View(songset.Where(x => x.Playlist.PlaylistName.Contains(search)).ToList());
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343