Jaeseo Lee
Jaeseo Lee

Reputation: 172

How to extract data from dictionary by LINQ

I make a dictionary for gathering some data. and extract from dictionary what I want. so I would like to use LINQ. However I don't know how to do that

I want to extract data from dictionary by LINQ and store as list

from textPoint1X, textPointY in textDataDictionary

where, range?
:2350 < textPoint1X < 2355 -300 < textPoint1Y < -298

// class for dictionary
   class textDataType
        {
            public string text { get; set; }
            public double textPoint1X { get; set; }
            public double textPoint1Y { get; set; }
        }

// declare a dictionary
var textDataDictionary = new Dictionary<UInt64, textDataType>();


// add data
textDataDictionary.Add(textData.Handle, 
                                    new textDataType
                                   {
                                       text = textData.Text,
                                       textPoint1X = textData.Point1.X,
                                       textPoint1Y = textData.Point1.Y
                                    });

Upvotes: 0

Views: 816

Answers (3)

Alsein
Alsein

Reputation: 4775

Here is the linq query writtin in syntax form:

var query =
from p in textDataDictionary
let x = p.Value.textPoint1X
let y = p.Value.textPoint1Y
where 2350 < x && x < 2355 && -300 < y && y < -298
select p.Value;
var result = query.ToList();

Upvotes: 1

Mark Cilia Vincenti
Mark Cilia Vincenti

Reputation: 1614

Here's some LINQ to help you. You need to filter using Where on the Values of the dictionary (to get a ValueCollection) and then finally convert it to a list as you want.

var myList = textDataDictionary.Values.Where(
    x => x.textPoint1X > 2350 &&
    x.textPoint1X < 2355 &&
    x.textPoint1Y > -300 &&
    x.textPoint1Y < -298).ToList();

Upvotes: 4

Rahul
Rahul

Reputation: 77876

You can use the dictionary Values property and use Linq Where() on it (example below)

textDataDictionary.Values.Where(x => x.textPoint1X > 2350 && x.textPoint1X < 2355).ToList(); 

Upvotes: 2

Related Questions