ANK
ANK

Reputation: 71

How to check if an item exist in list and has value

I have a model classes like below

public class A
{
 public string Name{get;set;}
 public string Value{get;set;}
}
public class B
{
 public List<A>Items{get;set;}
}

The below json will be deserialized to class B

{
"Items":[
{
  "Name":"Food Name"
  "Value":"Fries"
},
{
  "Name":"Weapon Name"
  "Value":"Gun"
},
{
  "Name":"Vehicle Name"
  "Value":"Car"
},
{
  "Name":"Pet Name"
  "Value":"Mewto"
},
{
  "Name":"Personal Name"
  "Value":"Leo"
}]}

Once the values are deserialized to the class B, I need to validate whether the list item contains Name Food Name,Personal Name,Weapon Name and Car Name and Value for these mentioned items are not null or empty. What is the best approach for this?

Upvotes: 0

Views: 2640

Answers (4)

Despacito 2
Despacito 2

Reputation: 494

string[] mandatoryNames = { "Food Name", "Personal Name", "Weapon Name", "Car Name" };
var valid = mandatoryNames
    .All(mandatoryName => items
        .Any(item => item.Name == mandatoryName
                     && !string.IsNullOrEmpty(item.Value)));

Upvotes: 1

Serge
Serge

Reputation: 43860

If it can be only one item of each name

var jsonParsed=JsonConvert.DeserializeObject<B>(json);

var valid=  jsonParsed.Items.Count(i=> names.Contains(i.Name) 
                && !string.IsNullOrEmpty(i.Value))==names.Length;

but if the several items are allowed, it will take 2 steps. At first we will check values, after this - names

var valid=false;
 
var notValidValues =jsonParsed.Items.Any(i=> names.Contains(i.Name) 
                          && string.IsNullOrEmpty(i.Value));
    
if (!notValidValues) valid = jsonParsed.Items.Where(i => names.Contains(i.Name))
                                    .GroupBy(i => i.Name).Count() == names.Length;

Upvotes: 2

swapmeet_Lou
swapmeet_Lou

Reputation: 292

Looks like you have a number solid solutions. I did something similar to Sami. Since you're deserializing to B, I created List to represent that. Not the most elegant, but it works. Hope you find what works well for you

//Sample list
var newB = new B
{
    Items = new List<A>
    {
        new A{Name = "Food Name", Value = "Fries" },
        new A{Name = "Weapon Name", Value = "Gun" },
        new A{ Name = "Weapon Name", Value = "" },
    }
};


foreach (var item in newB.Items)
{
   string[] vals = { item.Name, item.Value };   

   if (String.IsNullOrEmpty(vals[0]) || String.IsNullOrEmpty(vals[1]))
  {
    //Handle missing vals
    Console.WriteLine("We have a missing val");
  }
  else
  {
     Console.WriteLine("Good to go");
  }

}

Upvotes: 1

Sami
Sami

Reputation: 2110

You could go for something like

List<A> Items = //however you get the list
string[] names = {"Food Name","Personal Name","Weapon Name", "Car Name"};
foreach (string s in names)
{
    var item = Items.FirstOrDefault(i => i.Name == s);
    if (item == null || String.IsNullOrWhiteSpace(item.Value))
    {
        // handle the missing data
        break;
    }
}

This is likely an ugly solution, but it should work. Also take note, that this doesn't check for any other items in the list. You could have an item where Name is George and Value could be Potato, just as long as you also have the items with correct names.

Upvotes: 3

Related Questions