user2545071
user2545071

Reputation: 1410

c# Add some values at List

I write some code which do:

List<DataRow> rows=new <DataRow>();
foreach (DataRow dtRow in sqlRows)
{
    foreach (DataRow dtRowId in dttRows1)
    {
        if (Convert.ToInt32(dtRowId[0]) == Convert.ToInt32(dtRow[1]))
            rows.Add(dtParRow);
    }
}

Can I write it some more effective way? May be use LINQ? Or some other algorihtm?

P.S. sqlRows and dttRows1 is List. I grub it from table with query like this:"Select * from table name";

Upvotes: 0

Views: 119

Answers (3)

Cheranga
Cheranga

Reputation: 125

Looks like you need to find some items depending on a condition and add it to a separate list.

Try "join" in LINQ. Kindly have a look at the below example.

 public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

 var customerList1 = new List<Customer>(new[]
        {
            new Customer {Id = 1, Name = "AAA"},
            new Customer {Id = 1, Name = "BBB"},
            new Customer {Id = 1, Name = "CCC"}
        });

        var customerList2 = new List<Customer>(new[]
        {
            new Customer {Id = 1, Name = "AAA"},
            new Customer {Id = 1, Name = "BBB"},
            new Customer {Id = 1, Name = "CCC"},
            new Customer {Id = 1, Name = "DDD"}
        });

        var commonCustomers = customerList1.Join(customerList2, x => x.Name, y=>y.Name,(a,b)=>a).ToList();

So according to your question, what I think you should write is, (kindly note that the below code has not been tested)

var rows = sqlRows.Join(dttRows1, x => Convert.ToInt32(x[0]), y=>Convert.ToInt32(y[0]),(a,b)=>a).ToList();

Thanks, Cheranga

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500185

You could just use LINQ with a join, which would be more efficient than the nested approach:

var query = from dtRow in sqlRows
            join dtRowId in dttRows1
              on Convert.ToInt32(dtRow[1]) equals Convert.ToInt32(dtRowId[0])
            select ???; // dtParRow is unknown. Did you mean dtRow?
var list = query.ToList();

Note that if those values are already int values (so don't need parsing etc) then just cast instead:

var query = from dtRow in sqlRows
            join dtRowId in dttRows1
              on (int) dtRow[1] equals (int) dtRowId[0]
            select ???; // dtParRow is unknown. Did you mean dtRow?
var list = query.ToList();

Or use the Field<T> extension method (again, only if they're already int values which don't need converting):

var query = from dtRow in sqlRows
            join dtRowId in dttRows1
              on dtRow.Field<int>(1) equals dtRowId.Field<int>(0)
            select ???; // dtParRow is unknown. Did you mean dtRow?
var list = query.ToList();

Upvotes: 1

chfumero
chfumero

Reputation: 1147

You can rewrite that like

List<DataRow> rows= (from dtRow in sqlRows 
                     from dtRowId in dttRows1 
                     where Convert.ToInt32(dtRowId[0]) == Convert.ToInt32(dtRow[1])         
                     select dtParRow).ToList();

this is not always the better way. Yor code is a lot more clear.

Upvotes: 0

Related Questions