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 {...")
我在http://www.erictobia.com/2009/02/21/LoadADataTableWithLINQ.aspx上找到了以下示例。遗憾的是,我需要在VB中使用它,而且它使用的是我和自动代码转换器都不会重新识别的一些结构。那里的任何人都知道应该如何用VB.Net编写? (问题点是“选择新{...”)
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 个解决方案
#1
2
Looks to me to be case of using LINQ for the sake of using LINQ.
在我看来是为了使用LINQ而使用LINQ的情况。
Just for each context.Persons
只为每个context.Persons
For Each p As Person In context.Persons
ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName)
Next
#2
2
Anthony has given the correct answer. However, for the record: the new { … }
construct can be expressed in VB as follows:
安东尼给出了正确的答案。但是,对于记录:新的{...}构造可以用VB表示如下:
Dim result = From p As Person in context.Persons _
Select New With { _
.Row = ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName) _
}
#3
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();
#4
0
namespace TestBLL { public static class ConvertToDataTable { #region "Converting ObjectArray to Datatable"
namespace TestBLL {public static class ConvertToDataTable {#region“将ObjectArray转换为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
2
Looks to me to be case of using LINQ for the sake of using LINQ.
在我看来是为了使用LINQ而使用LINQ的情况。
Just for each context.Persons
只为每个context.Persons
For Each p As Person In context.Persons
ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName)
Next
#2
2
Anthony has given the correct answer. However, for the record: the new { … }
construct can be expressed in VB as follows:
安东尼给出了正确的答案。但是,对于记录:新的{...}构造可以用VB表示如下:
Dim result = From p As Person in context.Persons _
Select New With { _
.Row = ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName) _
}
#3
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();
#4
0
namespace TestBLL { public static class ConvertToDataTable { #region "Converting ObjectArray to Datatable"
namespace TestBLL {public static class ConvertToDataTable {#region“将ObjectArray转换为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
}
}
}