 |
|
สอบถามเรื่องการ convert Linq เป็น Dataset ไม่ทราบว่าสามารถทำได้หรือเปล่า |
|
 |
|
|
 |
 |
|
สอบถามการแปลงค่าจาก การใช้ Linq คิวรี่ ข้อมูลขึ้นมา แล้วเปลี่ยนค่านั้นเป็น ชนิด Dataset นะค่ะ ไม่ทราบว่าทำอย่างไรคะCode (C#)
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
};
จากตัวอย่างโค้ดข้างบน ถ้าเราต้องการแปลงค่า gr เป็น dataset เพื่อจะ return ค่าตัวนี้ไปใช้กับ method หนึ่งที่ต้องเอาไปเซตค่าแสดงใน gridview ไม่ทราบว่ามีวิธีทำหรือเปล่าอ่ะค่ะ
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), LINQ, C#
|
|
 |
 |
 |
 |
Date :
2012-08-01 16:09:38 |
By :
เด็กมีปัญหาต้องการคำตอบ |
View :
1402 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
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 ก็ได้ครับ
|
 |
 |
 |
 |
Date :
2012-08-01 17:25:31 |
By :
ch_b |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|