Jeff
Jeff

Reputation: 8138

Load DataTable Using Linq - Convert C# to VB.Net

I found the following example on http://www.erictobia.com/2009/02/21/LoadADataTableWithLINQ.aspx Unfortunately, I need it in VB and it's using some constructs that neither I nor the automated code converters reciognize. Anyone out there know how this should be written in VB.Net? (the problem spot is the "select new {...")

PeopleDataSet ds = new PeopleDataSet();
using (PeopleDataContext context = new PeopleDataContext())
{
    (from p in context.Persons
     select new
                {
                    Row = ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName)
                }).ToList();

}

Upvotes: 1

Views: 4873

Answers (4)

piyush
piyush

Reputation: 11

 tbl_EmployeeDetail obj_empdetails = new tbl_EmployeeDetail();
                obj_empdetails = Connection.obj_EmployeeDataClassesDataContext.tbl_EmployeeDetails.Single(m => m.EmployeeID == Convert.ToInt32(int_EmpID));
                obj_empdetails.FirstName = str_FirstName;
                obj_empdetails.LastName = str_LastName;
                obj_empdetails.DateOfJoining = dt_DateOfJoining;
                obj_empdetails.DepartmentID = int_DepartmentID;
                obj_empdetails.Designation = str_Designation;
                obj_empdetails.ExperienceInMonths = int_ExperienceInMonths;
                obj_empdetails.salary = dec_Salary;
                Connection.obj_EmployeeDataClassesDataContext.SubmitChanges();

Upvotes: 1

piyush
piyush

Reputation: 11

namespace TestBLL { public static class ConvertToDataTable { #region "Converting ObjectArray to Datatable"

    /// <summary>
    /// Method to Convert Datatable from object Array.
    /// </summary>
    /// <param name="array"></param>
    /// <returns></returns>
    public static DataTable ConvertToDatatable(this Object[] array)
    {

        PropertyInfo[] properties = array.GetType().GetElementType().GetProperties();
        DataTable dt = CreateDataTable(properties);
        if (array.Length != 0)
        {
            foreach (object o in array)
                FillData(properties, dt, o);
        }
        return dt;
    }

    /// <summary>
    /// Method To Create total column of datatable.
    /// </summary>
    /// <param name="properties"></param>
    /// <returns></returns>
    private static DataTable CreateDataTable(PropertyInfo[] properties)
    {
        DataTable dt = new DataTable();
        DataColumn dc = null;
        foreach (PropertyInfo pi in properties)
        {
            dc = new DataColumn();
            dc.ColumnName = pi.Name;
            //dc.DataType = pi.PropertyType;
            dt.Columns.Add(dc);
        }
        return dt;
    }

    /// <summary>
    /// Method for Fill data in DataTable.
    /// </summary>
    /// <param name="properties"></param>
    /// <param name="dt"></param>        
    private static void FillData(PropertyInfo[] properties, DataTable dt, Object o)
    {
        DataRow dr = dt.NewRow();
        foreach (PropertyInfo pi in properties)
        {
            dr[pi.Name] = pi.GetValue(o, null);
        }
        dt.Rows.Add(dr);
    }

    #endregion

}

}

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189447

Looks to me to be case of using LINQ for the sake of using LINQ.

Just for each context.Persons

For Each p As Person In context.Persons
   ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName)
Next

Upvotes: 2

Konrad Rudolph
Konrad Rudolph

Reputation: 545528

Anthony has given the correct answer. However, for the record: the new { … } construct can be expressed in VB as follows:

Dim result = From p As Person in context.Persons _
             Select New With { _
                 .Row = ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName) _
             }

Upvotes: 2

Related Questions