+ Start a Discussion
Sumesh ChandranSumesh Chandran 

How to filter rows from a list

I have a list here which has city name as a column, how to filter the rows by values from the city name column.
global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator('SELECT CITY_NAME__c,PROVINCE_CODE__c,STATUS__c,INTERNET_SERVICE__c,VIDEO_SERVICE__c,PHONE_SERVICE__c FROM MDU_Squad_Data_min__c');
    }
 
   // Execute method

    global void execute(Database.BatchableContext BC, List<MDU_Squad_Data_min__c> cityStats) {               
        // Here I am trying to filter the values of the cityStats list
    }

 
Best Answer chosen by Sumesh Chandran
Naga  AlapatiNaga Alapati
Hi Sumesh,

You can use the map to get all the records based on the city name as shown below
global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator('SELECT CITY_NAME__c,PROVINCE_CODE__c,STATUS__c,INTERNET_SERVICE__c,VIDEO_SERVICE__c,PHONE_SERVICE__c FROM MDU_Squad_Data_min__c');
    }
 
   // Execute method

global void execute(Database.BatchableContext BC, List<MDU_Squad_Data_min__c> cityStats) {               
        // Here I am trying to filter the values of the cityStats list
     Map<String, List<MDU_Squad_Data_min__c>> cityMduMap = new  Map<String,List<MDU_Squad_Data_min__c>>();
     for (MDU_Squad_Data_min__c m : cityStats ) {
        if (!cityMduMap.containsKey(m.CITY_NAME__c) {
           cityMduMap.put(m.CITY_NAME__c, new List<MDU_Squad_Data_min__c>());
        }
        cityMduMap.get(m.CITY_NAME__c).add(m);
     }
     

}

All Answers

Naga  AlapatiNaga Alapati
Hi Sumesh,

You can use the map to get all the records based on the city name as shown below
global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator('SELECT CITY_NAME__c,PROVINCE_CODE__c,STATUS__c,INTERNET_SERVICE__c,VIDEO_SERVICE__c,PHONE_SERVICE__c FROM MDU_Squad_Data_min__c');
    }
 
   // Execute method

global void execute(Database.BatchableContext BC, List<MDU_Squad_Data_min__c> cityStats) {               
        // Here I am trying to filter the values of the cityStats list
     Map<String, List<MDU_Squad_Data_min__c>> cityMduMap = new  Map<String,List<MDU_Squad_Data_min__c>>();
     for (MDU_Squad_Data_min__c m : cityStats ) {
        if (!cityMduMap.containsKey(m.CITY_NAME__c) {
           cityMduMap.put(m.CITY_NAME__c, new List<MDU_Squad_Data_min__c>());
        }
        cityMduMap.get(m.CITY_NAME__c).add(m);
     }
     

}
This was selected as the best answer
manish arorapnjmanish arorapnj
PNJ Sharptech is a leading Social Media Optimization company in India, specializing in handling both organic and paid Social Media Marketing (https://www.pnjsharptech.com/smo-services)(SMM) campaigns successfully. We have many years of experiencing increasing online social presence on various social media platforms such as Facebook, Twitter, LinkedIn and Pinterest, and many others. Our SMO experts have a rich knowledge of increasing traffic and maintaining the online social reputation for a long period. How our SMO services make you different from others? Our low-cost social media marketing services are very helpful to build your online reputation and increase sales.
Sumesh ChandranSumesh Chandran
Thanks @Naga Alapati. That was well explained.
Sumesh ChandranSumesh Chandran
Hello Nage, just checking can I write Apex batch classes in developer console.
Naga  AlapatiNaga Alapati
Yeah. You can write batch classes like other classes in developer console. If you want to execute that apex batch class, then you can use Database.executeBatch() method.


User-added image
Sumesh ChandranSumesh Chandran
Thanks Naga. 
Ash MartinAsh Martin
I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Thanks...
visit my blog for CPS Test (https://albertmartin.bcz.com/5-best-click-speed-test-to-check-your-click-speed/)
Edwin RobertEdwin Robert
If you want something entertaining for you then you should go for the best ache lagte ho hindi shayari (https://shayarikapitara.com/app-muskarate-huye-hi-ache-lagte-ho/) that is present with the hd images on the best shayari website in hindi - shayari ka pitara. These images can be shared with any of your cute ones. If you want to get this shayari then visit the official website of shayari ka pitara. If you want to start a new printing business then you should buy best quality uv printer at affordable uv printing machine price in india (https://phuvprinter.com/) from authorized uv printer supplier in India.
NRI Legal AdvisorsNRI Legal Advisors
Get the best and top nri legal services (https://nrilegaladvisorsindia.com/) india from the professional nri lawyers in India provide the best and free consultation. With the help of this free consultation you'll more understand your legal matter. If you are interested then hire RK Legal Advisors - best nri legal advisors in India. If you want to optimize your image for your website then visit the jpeg image optimizer online (https://digitalexpertsolution.com/jpeg-optimizer/) present over the official website of digital expert solution - best digital marketing expert in India.
Mark RohitMark Rohit
I think you can use artificial intellegense (https://macrowiz.com/) model to remove list
Sourabh Suman 17Sourabh Suman 17
Promote ABHI is the best social media marketing services agency in India (https://www.promoteabhi.com/social-media-optimization.php), we help- you gain visibility, your customers getting engaged through our informative, useful, creative content and your business getting the desired results from social media.
Sourabh Suman 17Sourabh Suman 17
SEO Services are essential when you are looking to rank your website on the search engine result pages. Promote Abhi, being the affordable SEO Company in Delhi (https://www.promoteabhi.com/seo-services.php), helps you with the result that you expect from a top SEO services company (https://www.promoteabhi.com/seo-services.php). What are you waiting for, COME ON NOW, FOLLOW MY LEAD!
Elanie BrownElanie Brown
I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thanks again I would like to refer to my blog about Fortnite Account Generator (https://sites.google.com/view/gaming-world-2021/Unlimited-Free-Fortnite-Accounts-for-PC-2021) You must read it at once. 
 
Sourabh Suman 17Sourabh Suman 17
Whether you are just a start-up or have an established business, our proficient team is capable of providing you best mobile app development services in India (https://www.promoteabhi.com/best-mobile-app-development-company.php).
Sourabh Suman 17Sourabh Suman 17
Your brand current and future success lie in the invaluable exercise that the top branding companies in India (https://www.promoteabhi.com/branding-company-delhi.php) do for you for building your brand strategy.
Siya Singh 8Siya Singh 8
If we are talking about the Best Website Development Service (https://planetaryaccountingandmarketing.com/development/). our company also working in India and internationally with active and good performances. You can visit for more information.