-3

I have a List which is getting filled correct, however after its being filled I want to remove all keys with a null value on a simple way, if exists, something like:

posts.RemoveAll(item => item == null);

I have multiple posts in this List<> For me the problem is to find the Key/Value to access as in a Dictionary.

Does somebody know the way to do this?

UPDATE:

my class looks like this:

public class iPost
{
    public int id { get; set; }
    public int post_origin_post_id { get; set; }
    public int pid { get; set; }
    public int container_type_id { get; set; }
    public int post_member_id { get; set; }
    public int post_toGroup_id { get; set; }
    public string title { get; set; }
    public string comment { get; set; }
    public string latitude { get; set; }
    public string longitude { get; set; }
    public string post_ip { get; set; }
    public bool canShared { get; set; }
    public bool isShared { get; set; }
    public int share_type_id { get; set; }
    public int views { get; set; }
user3763117
  • 327
  • 1
  • 5
  • 18
  • 3
    `"I have a List... I want to remove all keys with a null value"`. `List` doesn't have keys/values, only `T`s. You're going to need to show some sample code (a minimal working example) if you want any useful help. [`List.RemoveAll`](http://msdn.microsoft.com/en-us/library/wdka673a.aspx) takes a predicate, in which you can select objects to remove however you wish. – Jonathon Reinhart Oct 22 '14 at 03:26
  • It sounds like you want `posts.RemoveAll(item => item.key == null);`? But you would need to show your classname class. – Jesse Good Oct 22 '14 at 03:28
  • http://stackoverflow.com/questions/1636885/remove-item-in-dictionary-based-on-value – jay_t55 Oct 22 '14 at 05:47

1 Answers1

0

Where is your key? A c#-List has no keys.

Do you mean somethong like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    namespace DemoApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<DemoClass> list = new List<DemoClass>();

                list.Add(new DemoClass() { Key = "A1" });
                list.Add(new DemoClass() { Key = "A2" });
                list.Add(new DemoClass() { Key = "A3" });

                // Print
                foreach (var it in list) { Console.WriteLine(it.Key); }
                Console.WriteLine();

                // set one key to null
                list[1].Key = null;

                list.RemoveAll(Item => Item.IsToDelete());

                // Print
                foreach (var it in list) { Console.WriteLine(it.Key); }
                Console.WriteLine("As you can see, A2 is missing");

                Console.ReadLine();
            }
        }

        class DemoClass
        {
            public string Key
            {
                get;
                set;
            }

            public object SomeValue
            {
                get;
                set;
            }

            public bool IsToDelete()
            {
                if(Key == null || SomeValue == null)
                {
                     return true;
                }

                return false;
            }
        }
    }

Edit You can write an own attribute, which is placed over all members who are not allwed to be null. Than you can write an class which read this information and than decise if your instance ha to be removed from the list. Absract example:

[ProofForNull]
public string Key
{
   get;
   set;
}

[ProofForNull]
public string Value1
{
   get;
   set;
}

[ProofForNull]
public string Value2
{
   get;
   set;
}

Than you change IsToDelete(). It has to get all member via reflection and proof if they have the [ProofForNull] attribute. If one member with the attribute is null, IsToDelete() returns true. With this method you have one initial work to do, but the implementation for your classes is much easier.

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • As example but than with any object – user3763117 Oct 22 '14 at 08:13
  • @user3763117 I don't understand what you mean, **id** is your key? What is the problem? – BendEg Oct 22 '14 at 08:46
  • ITs the row key yes, but I want it to loop through all objects and checks if the object has a "null" value and remove this object. So in your example { Key = 1, description = "something", subject = null} I want it to remove "subject" – user3763117 Oct 22 '14 at 11:17
  • @user3763117 Than i would implement an IsToDelete() method. Look the updated example. – BendEg Oct 22 '14 at 11:48
  • The problem is that I have to define each object and as I have huge data liberies, I want to have more generic solution, so without key definition, but loops throught the List – user3763117 Oct 22 '14 at 11:56
  • @user3763117 I've edited my answer, it is no generic, but much simpler. – BendEg Oct 23 '14 at 07:17