Filtering an array in C# and classifying it

Ankit

Member
Joined
Dec 16, 2020
Messages
17
Programming Experience
3-5
Below I have an array of json coming from different data sources. and my job is to get ids for each item of json and pass them to different container based on what source it is coming. the way to differentiate here is that items coming from Source A will have properties Id,Project and ProjectId only and items with source other than A will have more properties like Id, Name, Project, Class etc. below code returns me the id if it coming from source A. I would like to figure out a way where I can get the ids coming from sources other than source A and also list which id comes from which Source. I am considering only two sources here one is source A(meaning items with properties Id,Project and ProjectId) while all other items as Source B. How do I extend the existing function to achieve my result?


C#:
private List<string> GetFilteredDocIdsFromSBMessage(string sbMessage)
        {
            var data = JsonConvert.DeserializeObject<List<JObject>>( sbMessage );
            List<string> filteredResult = new List<string>();
            foreach(var recordingData in data )
            {               
                string[] PropertiesFromSb = new string[3] { "id", "Project", "ProjectId" };
                JProperty[] jProperties = recordingData.Properties().ToArray();
                List<string> jPropertyName = new List<string>();
                string id = string.Empty;
                foreach( JProperty prop in jProperties )
                {
                    jPropertyName.Add( prop.Name );
                    if(prop.Name == "Id" )
                    {
                        id = prop.Value.ToString();
                    }
                }
                var isSourceA = true;
                foreach( string property in PropertiesFromSb )
                {
                    if (!jPropertyName.Contains(property))
                    {
                        isSourceA = false;
                    }
                }
                if( isSourceA )
                {
                    filteredResult.Add( id );
                    
                }
            }           
            
          
            return filteredResult;
        }
 
Seems like just a simple process of elimination. Go from the most discriminating criteria to the least discriminating to classify your data.

So are there truly only two sources: Source A and Source B? Or are there more than just two sources? If so, you told us how Source A is distinguished from the other sources, but how do the other sources distinguish themselves from each other?
 
Seems like just a simple process of elimination. Go from the most discriminating criteria to the least discriminating to classify your data.

So are there truly only two sources: Source A and Source B? Or are there more than just two sources? If so, you told us how Source A is distinguished from the other sources, but how do the other sources distinguish themselves from each other?
no as I mentioned, there is only two classifier.. one is source A and rest all be considered as Source B. Can you please elaborate on the process of elimination
 
Check jProperties.Count?
 
Here's the pseudo code:
C#:
IEnumerable<(string source, JObject item)> Classify(List<JObject> data)
{
    foreach(var recordingData in data)
    {
        var source = "sourceB";
        if (count == 3)
        {
            if (all properties match the names of the expected properties for source A)
                source = "sourceA";
        }
        yield return (source, recordingData);
    }
}
 

Latest posts

Back
Top Bottom