how to query in ef core?

mc 6,396 Reputation points
2023-08-15T14:30:27.5233333+00:00
from q in _context.Product
                               group q by q.SkuId into g
                               where g.Count(x => standardIds[0].Contains(x.StandardId)) == standardIds[0].Length
select g.Key;

I can do it using standardIds[0] but how to do it using standardIds?

standardIds is List<int>();

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-08-16T02:19:59.8366667+00:00

    Hi @mc

    I can do it using standardIds[0] but how to do it using standardIds? standardIds is List<int>();

    If the standardIds is a List<int>(), the standardIds[0] is an int type. Since the int does not contain a definition of "Contains" and "Length", it will throw a compilation error directly. Like this:

    User's image

    So, please check the standardIds again. it might not a List<int>().

    You can refer to the following code:

                List<int> standardIds = new List<int>() { 101, 102 };
    
                var result = (from q in _dbcontext.Product
                             group q by q.SkuId into g
                             where g.Count(x => standardIds.Contains(x.StandardId)) == standardIds.Count
                             select g.Key).ToList();
    
    
                var standardidarray =new int[]{ 101, 102}; 
                var items = new List<int[]>() { standardidarray };
                var result2 = (from q in _dbcontext.Product
                             group q by q.SkuId into g
                             where g.Count(x => items[0].Contains(x.StandardId)) == items[0].Length
                             select g.Key).ToList();
    

    The output as below:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.