---
title: "foreach (where x => x.PROPERTY), how to set PROPERTY?"  
description: "foreach (where x => x.PROPERTY), how to set PROPERTY?"  
author: "Anonymous User"  
published: 2014-08-19  
updated: 2014-08-19  
canonical: https://www.mindstick.com/forum/2061/foreach-where-x-x-property-how-to-set-property  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# foreach (where x => x.PROPERTY), how to set PROPERTY?

I have an Object [Student](https://www.mindstick.com/articles/157138/student-loan-default-wage-garnishment-and-student-loan), I get one of the [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp)'s [values](https://www.mindstick.com/forum/327/sum-textbox-values) by this [method](https://www.mindstick.com/forum/166/webservice-method) below

System.Reflection.PropertyInfo propValue = typeof(Student).GetProperty(s);

Let's say that s (the [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) I passed into the GetProperty) was the property called "StudentName". I would then like to run a [search](https://www.mindstick.com/articles/65368/best-smo-services-company-in-hyderabad-improve-search-rankings) based off that property, which was [stored](https://www.mindstick.com/forum/157561/what-is-the-stored-procedure-create-a-procedure-to-find-the-record-by-stu_id-from-the-student-table) in propValue, such as:

[foreach](https://www.mindstick.com/forum/33870/how-parallel-foreach-works-internally) (Student stu in formStudents.Where(x => x.propValue == "John"))

However this does not work, as x.__ only fills in with [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) of Student (even though valueProp contains a valid property of Student).

How can I override this so that is reads propValue as an actual [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of student, or what other method will work for me?

Thank you

## Replies

### Reply by Pravesh Singh

Hi Jeet, \

You call the .GetValue(...) method of the PropertyInfo object you got back from .GetProperty(s):

```
foreach (Student stu in formStudents){    var value = (string)propValue.GetValue(stu);    if (value == "John")    {        ....    }}
```

You can rewrite to LINQ if you want to:

```
var matchingStudents =    from stu in formStudents    let propertyValue = (string)propValue.GetValue(stu)    where propertyValue == "John";    select stu;foreach (var student in matchingStudents)
```


---

Original Source: https://www.mindstick.com/forum/2061/foreach-where-x-x-property-how-to-set-property

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
