Home > OS >  Filtering data based on matching ids present in a list using LINQ
Filtering data based on matching ids present in a list using LINQ

Time:05-20

I have written a program that will filter a list of data based on the list of ids coming from another list. After executing the LINQ query I want only those data that will be returned that only matches the data present in the filter criteria.

My program is like this

public class MarketplacePackageInfo
{
    public string PackageId { get; set; }
    public string PackageName { get; set; } 
    public List<AppInfo> Apps { get; set; }
}

public class AppInfo
{
    public string AppName { get; set; }    
    public string AppId { get; set; }
    
}


List<string>appIds=new List<string>();
appIds.Add("a1");
appIds.Add("a2");

List<MarketplacePackageInfo> packageList = new List<MarketplacePackageInfo>();
MarketplacePackageInfo pacakage1 = new MarketplacePackageInfo();
pacakage1.PackageId = "P1";
pacakage1.PackageId = "Package1";
pacakage1.Apps=new List<AppInfo>();
pacakage1.Apps.Add(new AppInfo { AppId = "a1",AppName="app1" });
pacakage1.Apps.Add(new AppInfo { AppId = "a2", AppName = "app2" });
pacakage1.Apps.Add(new AppInfo { AppId = "a3", AppName = "app3" });
packageList.Add(pacakage1);

MarketplacePackageInfo pacakage2 = new MarketplacePackageInfo();
pacakage2.PackageId = "P2";
pacakage2.PackageId = "Package2";
pacakage2.Apps = new List<AppInfo>();
pacakage2.Apps.Add(new AppInfo { AppId = "a1", AppName = "app1" });
pacakage2.Apps.Add(new AppInfo { AppId = "a2", AppName = "app2" }); 
packageList.Add(pacakage2);

MarketplacePackageInfo pacakage3 = new MarketplacePackageInfo();
pacakage3.PackageId = "P3";
pacakage3.PackageId = "Package3";
pacakage3.Apps = new List<AppInfo>();
pacakage3.Apps.Add(new AppInfo { AppId = "a2", AppName = "app2" });
pacakage3.Apps.Add(new AppInfo { AppId = "a3", AppName = "app3" });
packageList.Add(pacakage3);

MarketplacePackageInfo pacakage4 = new MarketplacePackageInfo();
pacakage4.PackageId = "P4";
pacakage4.PackageId = "Package4";
pacakage4.Apps = new List<AppInfo>();

pacakage4.Apps.Add(new AppInfo { AppId = "a2", AppName = "app2" });
pacakage4.Apps.Add(new AppInfo { AppId = "a3", AppName = "app3" });
pacakage4.Apps.Add(new AppInfo { AppId = "a4", AppName = "app4" });
packageList.Add(pacakage4);

var filterList= packageList.Where(e => e.Apps.Where(a => !appIds.Contains(a.AppId)).Count() > 0).ToList();

My desired output will be that I will get only package2 as both app is present there and no extra app is there. but it is returning data of Package1, package, and package4 which should not come. How to write that query?

CodePudding user response:

You can try following statements which give you same result:

var filterList = packageList.Where(e => e.Apps.Count(apps => !appIds.Contains(apps.AppId)) == 0).ToList();

or by using All which I think is better in terms of readability

var filterList = packageList.Where(e => e.Apps.All(apps => appIds.Contains(apps.AppId))).ToList();

They all give you packages (in this case package2) that include only apps in the appIds and no additional app.

  • Related