How to get records in EF that match a list of combinations (key/values)?

前端 未结 2 1837
日久生厌
日久生厌 2021-01-15 06:27

I have a database table with records for each user/year combination.

How can I get data from the database using EF and a list of userId/year combinations? S

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-15 06:45

    you can try this

    //add the possible filters to LIST
    var searchIds = new List { "1-2015", "1-2016", "2-2018" };
    
    //use the list to check in Where clause
    var result = (from x in YearResults
            where searchIds.Contains(x.UserId.ToString()+'-'+x.Year.ToString())
            select new UserYearCombination
            {
                UserId = x.UserId,
                Year = x.Year
            }).ToList();
    

    Method 2

    var d = YearResults
               .Where(x=>searchIds.Contains(x.UserId.ToString() + '-' + x.Year.ToString()))
               .Select(x => new UserYearCombination
                     {
                          UserId = x.UserId,
                          Year = x.Year
                     }).ToList();
    

提交回复
热议问题