Datatable Filtering

F

fzl2007

I would like to use a more concise method to do the following:

if (dt.Rows.Count < 3)
{

string expression;
expression = "customerName = 'John'";
DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.
foundRows = dt.Select(expression);
if (foundRows.Length == 0)
{
dt.Rows.Add("John", "0", "0");
}

string expressionq;
expressionq = "customerName = 'Jack'";
DataRow[] foundRowsq;

foundRowsq = dt.Select(expressionq);
if (foundRowsq.Length == 0)
{
dt.Rows.Add("Jack", "0", "0");
}


string expressionr;
expressionr = "customerName = 'Mary'";
DataRow[] foundRowsr;
foundRowsr = dt.Select(expressionr);

if (foundRowsr.Length == 0)
{
dt.Rows.Add("Mary", "0", "0");
}

}

Thanks,

Faye
 
J

Jeff Johnson

I would like to use a more concise method to do the following:

if (dt.Rows.Count < 3)
{

string expression;
expression = "customerName = 'John'";
DataRow[] foundRows;

// Use the Select method to find all rows matching the
filter.
foundRows = dt.Select(expression);
if (foundRows.Length == 0)
{
dt.Rows.Add("John", "0", "0");
}

string expressionq;
expressionq = "customerName = 'Jack'";
DataRow[] foundRowsq;

foundRowsq = dt.Select(expressionq);
if (foundRowsq.Length == 0)
{
dt.Rows.Add("Jack", "0", "0");
}


string expressionr;
expressionr = "customerName = 'Mary'";
DataRow[] foundRowsr;
foundRowsr = dt.Select(expressionr);

if (foundRowsr.Length == 0)
{
dt.Rows.Add("Mary", "0", "0");
}

}

Okay....

if (dt.Rows.Count < 3)
{
string[] names = { "John", "Jack", "Mary" };

for (int i = 0; i < 3; i++)
{
if (dt.Select(string.Format("customerName = '{0}'",
names)).Length == 0)
{
dt.Rows.Add(names, "0", "0");
}
}
}

If that's not what you really wanted, perhaps you could be a little more
specific with the question.
 
A

Arne Vajhøj

I would like to use a more concise method to do the following:

if (dt.Rows.Count < 3)
{

string expression;
expression = "customerName = 'John'";
DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.
foundRows = dt.Select(expression);
if (foundRows.Length == 0)
{
dt.Rows.Add("John", "0", "0");
}

string expressionq;
expressionq = "customerName = 'Jack'";
DataRow[] foundRowsq;

foundRowsq = dt.Select(expressionq);
if (foundRowsq.Length == 0)
{
dt.Rows.Add("Jack", "0", "0");
}


string expressionr;
expressionr = "customerName = 'Mary'";
DataRow[] foundRowsr;
foundRowsr = dt.Select(expressionr);

if (foundRowsr.Length == 0)
{
dt.Rows.Add("Mary", "0", "0");
}

}

I don't think there are any fundamentally better method.

But it could be structured a little bit better.

Demo:

using System;
using System.Data;

namespace E
{
public class Program
{
public static DataTable Create()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Value", typeof(int));
dt.Rows.Add("John", 123);
dt.Rows.Add("Alexander", 456);
dt.Rows.Add("Bill", 789);
return dt;
}
public static void Print(DataTable dt)
{
foreach(DataRow dr in dt.Rows)
{
foreach(object o in dr.ItemArray)
{
Console.Write(" " + o);
}
Console.WriteLine();
}
}
public static void Original(DataTable dt)
{
string expression = "Name = 'John'";
DataRow[] foundRows = dt.Select(expression);
if (foundRows.Length == 0)
{
dt.Rows.Add("John", 0);
}
string expressionq = "Name = 'Jack'";
DataRow[] foundRowsq = dt.Select(expressionq);
if (foundRowsq.Length == 0)
{
dt.Rows.Add("Jack", 0);
}
string expressionr = "Name = 'Mary'";
DataRow[] foundRowsr = dt.Select(expressionr);
if (foundRowsr.Length == 0)
{
dt.Rows.Add("Mary", 0);
}
}
private static void AddIfNotExists(DataTable dt, string name)
{
if(dt.Select("Name='" + name + "'").Length == 0)
{
dt.Rows.Add(name, 0);
}
}
public static void Suggest(DataTable dt)
{
AddIfNotExists(dt, "John");
AddIfNotExists(dt, "Jack");
AddIfNotExists(dt, "Mary");
}
public static void Main(string[] args)
{
DataTable dt1 = Create();
Original(dt1);
Print(dt1);
DataTable dt2 = Create();
Suggest(dt2);
Print(dt2);
Console.ReadKey();
}
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top