I have summarized my problem in following c# code snippet.In which I am filtering hotel by review.we get user review from user in the from of comma separated value.Currently I am looping through the user review and combining each result into single result. Now,I want to write loop and linq query into single linq query.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqST
{
public class Hotel
{
public string Name { get; set; }
public int Review { get; set; }
}
public class DAL
{
public static List<Hotel> Get()
{
return new List<Hotel>()
{
new Hotel{Name="Hotel1",Review=4},
new Hotel{Name="Hotel2",Review=2},
new Hotel{Name="Hotel3",Review=2},
new Hotel{Name="Hotel4",Review=3},
new Hotel{Name="Hotel5",Review=5},
new Hotel{Name="Hotel6",Review=5},
new Hotel{Name="Hotel61",Review=4},
new Hotel{Name="Hotel6",Review=4}
};
}
}
class Program
{
static void Main(string[] args)
{
var filterHotel = "3,4";
var tempList = new List<Hotel>();
foreach (var item in Array.ConvertAll(filterHotel.Split(','), s => int.Parse(s)))
{
var list = DAL.Get().Where(x => x.Review == item);
tempList = tempList.Union(list).ToList();
}
}
}
}