caldera.sac
caldera.sac

Reputation: 5108

How to filter a data table with each items in a object using RowFilter in c#

I have a data table and I can filter it like this.

DataView dv = new DataView(table);
dv.RowFilter = string.Format("[SAILING-DATE]='{0}'", "03/12/16");//step ONE

this returns result successfully.

now I have a object like this

var myObject = returnPorts(one).Split('|');

this myObject includes 5 items. so I want to filter above DataTable for all items in the myObject.I don't know how to do that.need like this

 dv.RowFilter = string.Format("[SAILING-PORT]='{0}'", "Should filter with all the items in the myObject at once");//step TWO

want your help with this. and other thing is I want to do step ONE and step TWO at once.need the help for that also. thank you very much.

Upvotes: 1

Views: 849

Answers (3)

caldera.sac
caldera.sac

Reputation: 5108

This is the complete answer. @freedomn-m's answer helped me lot.thank your very much.

For the first part(filter with any number of items in the object).thanx to @freedomn-m

var parts = returnPorts(one).Split('|');
dv.RowFilter = string.Format("[SAILING-PORT] IN ('{0}')", String.Join("','", parts));

For part two - to concate any number of conditions

dv.RowFilter = string.Format("[SAILING-PORT] IN  ('{0}') AND [CRUISE-ONLY]=('{1}') AND [SAILING-DATE]=('{2}')", String.Join("','", threee), "YES", "03/12/16");

thanx all, who helped me.

Upvotes: 0

Balagurunathan Marimuthu
Balagurunathan Marimuthu

Reputation: 2978

dv.RowFilter = string.Format("[SAILING-PORT] in ('{0}')", string.Join(" ',' ", myObject ));

this will concatenate all the values from the array into single string. This will allow you to filter all the values from in the array.

Upvotes: 0

fdomn-m
fdomn-m

Reputation: 28621

RowFilter uses as fairly basic syntax but does allow you to use IN:

var parts = returnPorts(one).Split('|');
dv.RowFilter = string.Format("[SAILING-PORT] IN ('{0}')", String.Join("','", parts));

to combine this with the date, either add it to parts or, cleaner, to the format:

var parts = returnPorts(one).Split('|');
dv.RowFilter = string.Format("[SAILING-PORT] IN ('{0}', '{1}')", 
                             "03/12/16", 
                             String.Join("','", parts));

Upvotes: 1

Related Questions