1

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();

}

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Yep. Assuming the code in the question is complete and nothing is ever done with the List<T> created with ToList(), there's no need to use LINQ.
Don't declare p before the scope of the loop, though. Declare it in the loop head instead (For Each p [As Person] in context.Persons).
Thanks, Konrad I've tweaked the answer, suffice to say I don't spend a great deal of time in VB.NE.
2

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) _
             }

Comments

1
 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();

Comments

0

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

}

}

1 Comment

EmployeeDataClassesDataContext obj_empcontext = new EmployeeDataClassesDataContext(); tbl_EmployeeDetail obj_emp = obj_empcontext.tbl_EmployeeDetails.First(p => p.EmployeeID == int_EmpID); obj_empcontext.tbl_EmployeeDetails.DeleteOnSubmit(obj_emp); obj_empcontext.SubmitChanges();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.