To get the specific properties from list

My list contents are as follows,

List<MyData> DICT_list = new List<MyData>()
{
   new MyData{ Dictionay="Alpha", Dimension="Length", Classes=null},
   new MyData{ Dictionay="Alpha", Dimension="Length", Classes="Process"},
   new MyData{ Dictionay="Alpha", Dimension="breath", Classes="Activity"},
   new MyData{ Dictionay="Alpha", Dimension="Height", Classes="Workflow"},
   new MyData{ Dictionay="Beta", Dimension="Height", Classes=null},
   new MyData{ Dictionay="Beta", Dimension="Height", Classes="Workflow"},
   new MyData{ Dictionay="Beta", Dimension="Length", Classes="Workflow"}
};

I want to have the data for eg as below,

 Want to get All the Classes of Dimension=length in dictionary=ALPHA only...

Any help will be greatly appreciated.

---------Updation in question-------

public struct MyData
        {
            public string Dictionary { get; set; }
            public string Dimension { get; set; }
            public string Classes { get; set; }
        }

and executing as below,

var SelectedValue = DICT_list.Where(d => d.Dimension == "Length" && d.Dictionary == "Alpha").Select(d => d.Class);
                foreach (var value in SelectedValue)
                {
                    MessageBox.Show(value.ToString());
                }

I am getting following error at line MessageBox.show(value.ToString); as

Object Reference not set to instance of an object

And I my list contains the desired element, I have tested it by following command,

string count = DICT_list.Count.ToString(); // Here count gives the value 14 which has the desired Dictionary and DImension name along with several classes which I need to fetch..

-------------------Updated question---------

I guess I know why this error(if I am not wrong) is coming because My first value for classes for new dimension is null in List, I am sorry for wrong question, got to know now by testing, can I do something which will ignore the null value and move for next from list..

Answers


First - simply filter list by Dimension and Dictionay. Second - project each item from result by selecting Classes only. That will give you IEnumerable<string>:

var query = DICT_list.Where(d => d.Dimension == "Length" && d.Dictionay == "Aplha")
                     .Select(d => d.Classes);

Or query syntax:

var query = from d in DICT_list
            where d.Dimension == "Length" && d.Dictionay == "Aplha"
            select d.Classes;

I suggest you to take look on Basic LINQ Query Operations (C#)


The Solution given by @Sergey Berezovskiy works properly. here is a proof for that.

class Program
{
    static void Main(string[] args)
    {

        List<MyData> DICT_list = new List<MyData>()
        {
           new MyData{ Dictionay="Aplha", Dimension="Length", Classes="Process"},
           new MyData{ Dictionay="Aplha", Dimension="breath", Classes="Activity"},
           new MyData{ Dictionay="Aplha", Dimension="Height", Classes="Workflow"},
           new MyData{ Dictionay="Beta", Dimension="Height", Classes="Workflow"},
           new MyData{ Dictionay="Beta", Dimension="Length", Classes="Workflow"}
        };

        var query = DICT_list.Where(d => d.Dimension == "Length" && d.Dictionay == "Aplha")
                 .Select(d => d.Classes);

        foreach (var VARIABLE in query)
        {
            Console.WriteLine(VARIABLE.ToString());
        }


        Console.ReadLine();
    }
}

OUTPUT : Process

Extented answer for your later question : "can I do something which will ignore the null value and move for next from list.."

This query might help you:

var query = DICT_list.Where(d => d.Dimension == "Length" && d.Dictionay == 
"Aplha" && d.Classes != null).Select(d => d.Classes);

Need Your Help

FactoryGirl: has_many :through, validates_presence_of: association error: can't be blank?

ruby-on-rails ruby-on-rails-4 factory-girl rspec-rails rspec3

I'm receiving the below test failure on my Article factory, which has a has_many through:, validates_presence_of relationship with my Categories model. The Categories need to exist before the Artic...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.