Home > Enterprise >  Is there a way to return a list of records which match an attribute of the first query filter result
Is there a way to return a list of records which match an attribute of the first query filter result

Time:03-12

I have a list of records where their uniqueness is based of a string (name) and languageID (ForeignKey)

I have the ID (PK) which I use in the query filter e.g.

Video = _videoRepository.Get(v => v.ID == parameterID && v.Guid == ` 
_currentUser.Guid).FirstOrDefault();`

This of course will return one matching record, however I have a use case where I have multiple 'Videos' with the same name but different languageIDs, and in this case would like to return all of them e.g.

Name = '*videoName*', LanguageID = 1,
Name = '*videoName*', LanguageID = 2,
Name = '*videoName*', LanguageID = 3,
Name = '*videoName*', LanguageID = 4...

So essentially want to be able to return the one matching the 'parameterID' as well as all those which match its name attribute.

I know this could be done using separate repository calls, getting the first match and then using it to return any records which match its name but I would like to do this in the same query. Any help would be hugely appreciated!

CodePudding user response:

say you have a collection, like a List videoList = new List();

That you yanked out of a _videoRepository.GetAll()? Maybe ?

all you would need to do is:

var subSet = videoList.Where(v => v.Name.Equals("NameToSearchFor"));

now if you want to do it directly on your repository, it isn't harder than implementing a GetBy() parameter, that returns a list, and then correlating it to an implementation on your IO component that gets a list.

CodePudding user response:

If I understand you correctly you would like to find all IDs that match the VideoName. Grouping the data by VideoName in that case is the solution. The code below will result in one item that contains all the LanguageIDs for the VideoName. Is that what you are looking for?

using System;
using System.Collections.Generic;
using System.Linq;

namespace RemoveChars
{
    internal class Program
    {
        /// <summary>
        /// Class holding your videonames and languageID
        /// </summary>
        public class Item
        {
            public string Name { get; set; }    
            public int LanguageID { get; set; }
        }
        static void Main(string[] args)
        {
            // Creating a list of all Videonames and LanguageIDs
            List<Item> list = new List<Item>();

            list.Add(new Item { Name = "*videoName*", LanguageID = 1 });
            list.Add(new Item { Name = "*videoName*", LanguageID = 2 });
            list.Add(new Item { Name = "*videoName*", LanguageID = 3 });
            list.Add(new Item { Name = "*videoName*", LanguageID = 4 });

            // Now get a result grouping the list by its item.Name (videoname).
            var result = list.GroupBy(item => item.Name).ToList();

            // Printout the result
            foreach (var item in result)
            {
                // The Key holds the videoname
                Console.WriteLine(item.Key);

                // The item holds the IDs in that group.
                foreach (var id in item)
                {
                    Console.WriteLine(id.LanguageID);
                }
            }
            
            Console.ReadKey();
        }
    }
}

Result is:

*videoName*
1
2
3
4
  • Related