Casting Var to DataTable type RRS feed

  • Question

  • Hello everyone,

    Is there a way to cast a var querry without using foreach loop?


     var querry =

    from q in this.database.datatable

    where q.Id1 == XXX    // Id1 is a foreign key

    select q;

    if (querry.Count() > 0)


    foreach (var index in querry)


    refVariable = index.Id2; // Id2 is the primary key



    Can I get index.Id2 without looping?





    Friday, September 24, 2010 3:39 AM


All replies

  • Hello there,


    A sample Code is given here

    if(Result.Count() > 0){
        DataTable objtable = Result.CopyToDataTable<DataRow>();


    Link is given below

    C# DataTable Foreach Loop

    Hope this helps.



    Phijo Mathew Philip

    Friday, September 24, 2010 5:12 AM
  • "Can I get index.Id2 without looping?"

    Not sure I understand ththe question and this part makes me think "why?"

    You can specify a list of fields and alias them.

    There are also a number of extension methods available and you can write your own.  The latter would enable you to run code for each of those entries without looping.


    Friday, September 24, 2010 12:44 PM