Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter dynamically using c# xelement?

I have done my searching and was not able to find a solution to problem that I have.

I'm a bit new to c#.net.

Here is my problem. I am trying to dynamically filter xelement.

The number of attribute and the value of the attributes are not known, and will depend on some other routine/process.

these my attribute name to filter, can be one or more attribute to filter.

string[] param = new string[] { "techcode", "productgroup", "photolayer" }

my xml file is in this form:

<?xml version="1.0" encoding="utf-8"?>
<threads>
  <thread techcode="sometech" productgroup="pgroup" 
          photolayer="player" biasewma="-0.05" />
</threads>

I can filter succesfully if i hardcoded something like this

IEnumerable<XElement> singlethread = (from el in apcxmlstate.Elements("thread")
  where 
    (string)el.Attribute("techcode") == somevalue
    && (string)el.Attribute("productgroup") == somevalue
    && (string)el.Attribute("photolayer") == somevalue
  select el);

However, this is not what I want, because I will not know which attribute do I exactly want to filter. It will be generated dynamically.

For example, at run time, attribute that I want to filters are techcode and productgroup only. Would any kind soul help me to provide suggestion.

like image 228
Amir Ismail Avatar asked Jul 29 '13 07:07

Amir Ismail


People also ask

How to filter data automatically with dynamic arrays in Excel?

Filtering your data is one of the most common tasks with Excel. And this task is very simple when you click on the little arrow in the header. Now, with the dynamic arrays functions, it is possible to create a workbook where you can filter your data automatically in function of the values of your criteria.

Can I build complicated filters with the dynamicfilterbuilder?

Of course, all the fields, values and operators can be provided by a user/frontend and it’ll just work! The DynamicFilterBuilder lets you build complicated filters, which gives you everything you need for a fully dynamic filtering system!

What is the use of filter in Excel?

The FILTER function in Excel is used to filter a range of data based on the criteria that you specify. The function belongs to the category of Dynamic Arrays functions. The result is an array of values that automatically spills into a range of cells, starting from the cell where you enter a formula.

How to filter data by a single column or multiple columns?

where array identifies the source data, include identifies the value (s) you want to see in the filtered data set, and the optional if_empty specifies the value to display when the result is an empty set. You can use FILTER () to return a single column or several. In addition, you can filter by a single column or several.


1 Answers

You can build query dynamically:

IEnumerable<XElement> query = apcxmlstate.Elements("thread");

foreach(var name in param)
   query = query.Where(t => (string)t.Attribute(name) == someValue);

UPDATE: I think your problem is that instead of single someValue variable you are trying to get different values for each attribute. But only last one is captured in lambda. You need to create local variable to store value for each lambda:

IEnumerable<XElement> singlethread = apcxmlstate.Elements("thread"); 

foreach (var name in param) {
   var value = row[name].ToString();
   singlethread = singlethread.Where(t => (string)t.Attribute(name) == value); 
}
like image 161
Sergey Berezovskiy Avatar answered Sep 21 '22 13:09

Sergey Berezovskiy