# How to Get Property Values using LINQ to Objects

by Zoran Horvat

Reflection lets us access members of an object, including property getters and setters. Through specialized methods we can read values from properties and write values back into them.

LINQ to Objects does not add any special ability regarding getting and setting property values. But LINQ makes it easy to filter the type members according to various criteria.

The following piece of code selects all read/write properties of integer type and converts them to name/value pairs. Name in each pair is the property name, while value is the actual value of the property. Here is the code:

``````IEnumerable<Tuple<string, int>> DehydrateObject(object obj)
{

IEnumerable<Tuple<string, int>> values =
(from property in obj.GetType().GetProperties()
where property.PropertyType == typeof(int) &&
property.CanWrite
select new Tuple<string, int>(property.Name,
(int)property.GetValue(obj)));

return values;

}
``````

## Example

Below is the source code of a console application which demonstrates the DehydrateObject method. When function listed above is applied to an instance of the Rectangle structure, it extracts four integer properties that hold rectangle's location and size:

``````using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace PropertyGettingDemo
{
class Program
{

static IEnumerable<Tuple<string, int>> DehydrateObject(object obj)
{

IEnumerable<Tuple<string, int>> values =
(from property in obj.GetType().GetProperties()
where property.PropertyType == typeof(int) &&
property.CanWrite
select new Tuple<string, int>(property.Name,
(int)property.GetValue(obj)));

return values;

}

static void Main(string[] args)
{

Rectangle rect = new Rectangle(20, 30, 150, 90);

IEnumerable<Tuple<string, int>> values = DehydrateObject(rect);
Console.WriteLine("Rectangle={0}", rect);

foreach (Tuple<string, int> value in values)
Console.WriteLine("{0}={1}", value.Item1, value.Item2);

Console.Write("Press ENTER to continue... ");

}
}
}
``````

The code produces output like this:

```            ```
Rectangle={X=20,Y=30,Width=150,Height=90}
X=20
Y=30
Width=150
Height=90
Press ENTER to continue...
```
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.8175521,"math_prob":0.86006963,"size":4433,"snap":"2022-40-2023-06","text_gpt3_token_len":876,"char_repetition_ratio":0.12102055,"word_repetition_ratio":0.099673204,"special_character_ratio":0.20933905,"punctuation_ratio":0.17326732,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9674606,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-03T17:52:12Z\",\"WARC-Record-ID\":\"<urn:uuid:04e36e36-322f-43f3-88d6-7e2139eb5bf3>\",\"Content-Length\":\"26456\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e937ec13-93af-4812-ae96-8e6b08d5cec5>\",\"WARC-Concurrent-To\":\"<urn:uuid:71269e6a-df96-4835-9acf-dc495332934a>\",\"WARC-IP-Address\":\"52.171.136.200\",\"WARC-Target-URI\":\"https://codinghelmet.com/articles/linq-property-getter\",\"WARC-Payload-Digest\":\"sha1:HYK6QL2VCYEOV74627SQNAY5L7JX3VVN\",\"WARC-Block-Digest\":\"sha1:7PVWL2VQOZ362BHQ2QD3OVCXPW47R3SL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337428.0_warc_CC-MAIN-20221003164901-20221003194901-00531.warc.gz\"}"}