Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
LINQ - Return data from list 2 occurrences

I have a list of int called "motor_list" = 2, 3, 5, 3, 8, 5, 5, 9, 8 (List 2)

(3 = 2 times in the list)
(5 = 3 times)

I have a table (Item), which content row "ItemId, Name, Date, Location", and it's only content 2 datas :

(ItemId) = 3 and 5. (List 1)

var data = _context.Item.Where(x => motor_list.Contains(x.ItemId));

Using the above code, I'm only getting two data which is obviously right technically,

now I want to get data but according to the occurrence of motor_list (List 2), and no the one for Item table.

I mean I want to get something like :

ItemId 3 should pull 2 times the same data, and
ItemId 5 should pull 3 times.
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Try this code

    List<int> list1 = new List<int>() { 3, 5 };
    List<int> list2 = new List<int>() { 2, 3, 5, 3, 8, 5, 5, 9, 8 };

    var data = list2.GroupBy(x => x).Where(a => list1.Contains(a.Key));

   var result=   data.Select(g => $"ItemId {g.Key} should pull {g.Count()} times").ToList();

foreach (var item in result)
{
    Console.WriteLine(item);
}
over 4 years ago · Santiago Trujillo Report

0

Use join to return exactly the items in the motor_list.

var data = motor_list
           .Join(_context.Item,
           left => left,
           right => right.ItemId,
           (left, right) => new { items = right }); 

or a little further away

var data = _context.Item.ToList();
var result = lst.Select(x => new
     {
        item = data.Where(a => a.ID == x).FirstOrDefault()
     }).Where(a=>a.item != null).ToList();
over 4 years ago · Santiago Trujillo Report

0

I'm not sure what you mean by "ItemId 3 should pull 2 times the same data". It sounds like you either want the count of each ItemId in motor_list, or you want the duplicate items returned in the results.

Here's how you can accomplish either:

class Item
{
    public int ItemId { get; set; }
}

static void Main(string[] args)
{
    List<Item> contextItems = new List<Item> { new Item { ItemId = 3 }, new Item { ItemId = 5 } };

    var motor_list = new List<int> { 2, 3, 5, 3, 8, 5, 5, 9, 8 };

    // To get the count of each contextItem in motor_list:
    var itemCount = contextItems
            .Select(contextItem => 
                motor_list.Count(motorId => motorId == contextItem.ItemId))
            .ToList();

    // result: { 2, 3 }

    // To get duplicate items for each contextItem in motor_list
    var items = motor_list
            .Where(motorId => 
                contextItems.Any(contextItem => contextItem.ItemId == motorId))
            .ToList();

    // result: { 3, 5, 3, 5, 5 }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!