BisimoWHLINQDataContext dbConnect = new BisimoWHLINQDataContext();
var gr = from c in dbConnect.GoodsReceives
join address in dbConnect.AddressBusinessPartners on c.AddressBusinessPartnerID equals address.AddressBusinessPartnerID
join w in dbConnect.Warehouses on c.WarehouseID equals w.WarehouseID
join d in dbConnect.DocumentTypes on c.DocumentTypeID equals d.DocumentTypeId
join o in dbConnect.Organizations on c.OrganizationID equals o.OrganizationID
join b in dbConnect.BusinessPartners on address.BusinessPartnerID equals b.BusinessPartnerID
join address2 in dbConnect.AddressBusinessPartners on c.DeliveryLocationID equals address2.AddressBusinessPartnerID
orderby c.CreateDate descending
select new
{
GoodsReceiveID = c.GoodsReceiveID,
SearchKeyAddressDelivery = address.SearchKey,
OrganizationName = o.OrganizationName,
GoodsReceiveOrder = c.GoodsReceiveOrder,
AddressBusinessPartnerID = c.AddressBusinessPartnerID,
MovementDate = c.MovementDate,
BusinessPartnerName = b.BusinessPartnerID,
DocumentTypeName = c.DocumentTypeID,
SearchKeyAddress = address.SearchKey,
BusinessPartnerID = address.BusinessPartnerID,
Description = c.Description,
GoodsReceiveOrganizationID = c.OrganizationID,
WarehouseName = w.WarehouseName,
DocumentStatus = c.DocumentStatus
};
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), LINQ, C#
Date :
2012-08-01 16:09:38
By :
เด็กมีปัญหาต้องการคำตอบ
View :
1368
Reply :
1
No. 1
Guest
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
public static class ObjectToDatatable
{
public static DataTable ListToDataTable<T>(List<T> list)
{
DataTable dt = new DataTable();
foreach (PropertyInfo info in typeof(T).GetProperties())
{
dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
foreach (T t in list)
{
DataRow row = dt.NewRow();
foreach (PropertyInfo info in typeof(T).GetProperties())
{
row[info.Name] = info.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
}
ใช้งานก็
Code (C#)
var ReceiptQuery = db.Receipts;
DataSet d = new DataSet();
DataTable dtTemp = ObjectToDatatable.ListToDataTable(ReceiptQuery.ToList());
d.Tables.Add(dtTemp);
ข้อจำกัดคือจะมีปัญหากับค่า Null ของ Record ครับ หรือไม่ก็ลองหา google ก็ได้ครับ