[SalesForce] unexpected token: IN in dynamic soql

I have written a code which will give my dynamic soql and using this soql my execution will work. But i am getting this erro

unexpected token: IN41

Below is my code
Code

public class DateLastUpdateUtil{

    public static void findObjectName (List<String> ListrecordIdPrefix){

        String objectName = '';
        List<String> Matchingrecord = new List<String>();
        List<Id> ListOfIds = new List<Id>(); 
        try{

            for(String recordId : ListrecordIdPrefix){
                //Get prefix from record ID
                //This assumes that you have passed at least 3 characters
                String myIdPrefix = String.valueOf(recordId).substring(0,3);
                Map<String,Schema.sObjectType> MapSObjectType = Schema.getGlobalDescribe();

                for(Schema.sObjectType stype : MapSObjectType.values()){
                    Schema.DescribeSObjectResult type = stype.getDescribe();
                    String prefix = type.getKeyPrefix();


                    if(prefix != null && prefix.equals(myIdPrefix)){
                        objectName = type.getName();
                        ListOfIds.add(Id.valueOf(recordId));
                        system.debug('objectName======>'+objectName);
                        DateLastUpdateUtil.dateDefine(objectName,ListOfIds);
                        break;
                    }
                }

            } 

        }Catch(Exception e){
            system.debug('Exception due to==========>'+e.getMessage()+e.getLineNumber());
        }  

    }

    public static void dateDefine (String objectName,List<Id> ListrecordIdPrefix){
        // String queryStr='SELECT,id,LastModifiedDate FROM'+objectName+'WHERE ID IN:'+ListrecordIdPrefix;
        List<sObject> records = Database.query('Select Id,LastModifiedDate From ' + objectName +'WHERE id IN : ListrecordIdPrefix'); //getting error on this line
        for(SObject record: records){
            DateTime objDT = DateTime.valueOf(record.get('LastModifiedDate'));
            DateTime currentTime = dateTime.now();
            Long dt1 = objDT.getTime();
            Long dt2 = currentTime.getTime();

            Long milliseconds  = dt2-dt1;

            Long Seconds = milliseconds/1000;
            Long Minutes = seconds/60;
            Long Hours = Minutes/60;
            Long Days = Hours/24;

            system.debug('seconds====>'+Seconds);
            system.debug('Minutes====>'+Minutes);
            system.debug('Hours======>'+Hours);
            system.debug('Days======>'+days);

            if(Seconds <= 60){
                system.debug('Last updated a few seconds ago');
            }else if(Minutes <= 60){
                system.debug('Last updated a few minutes ago');
            }else if(Hours >= 24){
                seconds = Math.Mod(seconds,60);
                minutes = Math.Mod(minutes,60);
                hours = Math.Mod(minutes,60);
                system.debug('Last updated a Yesterday '+hours+':'+Minutes+':'+Seconds);
            }else if(Days <= 6){
                seconds = Math.Mod(seconds,60);
                minutes = Math.Mod(minutes,60);
                hours = Math.Mod(minutes,60);
                Days = Math.mod(Hours,24);
                system.debug('Last Update on a '+days+' '+hours+':'+Minutes+':'+Seconds);
            }else{
                system.debug('Last Updated on =====>'+objDT.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\''));
            }
        }

    }    
}

Thanks for your help in advance !!!

Best Answer

Your posted error goes with the commented line for the query so it does not match your posted code

In your current code you are Missing a space in your query

 objectName +'WHERE

To

 objectName +' WHERE

Before the where

In the commented query that matches the error given in the question you found that using the property that way was wrong. But From the naming and the error your the value of the property concerns me as

Listrecordidprefix

May be a list of the first 3 characters of the id. You need to be using a list of full ids.

Since you have not shown how you constructed that list it is hard to say what you need to do to fix it.