public void TEST(SEPM.SEPMDataContext oDC)
{
//一般 LINQ 寫法, 欄位只能寫死, 如:
var Q = query.OrderBy(r => r.statusId).ToList();
//若 OrderBy 的欄位不是固定的
//方法一:條列
var orderBy = "XXX";
if (orderBy == "ownerId")
{
Q = Q.OrderBy(r => r.ownerId).ToList();
}
else if (orderBy == "endDate")
{
Q = Q.OrderBy(r => r.endDate).ToList();
}
//方法二:
Q = Q.OrderBy(r => GetPropertyValue(r, orderBy)).ToList();
}
private object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}
Random rand = new Random(Guid.NewGuid().GetHashCode());
List<int> listLinq = new List<int>(Enumerable.Range(0, dt.Rows.Count - 1));
listLinq = listLinq.OrderBy(num => rand.Next()).ToList<int>();
foreach (DataRow oRow in DT.Rows)
{
var oSP = new DB.StorePointRecord(oRow);
:
:
}
var oSP = new DB.StorePointRecord();
foreach (DataRow oRow in DT.Rows)
{
oSP.row = oRow;
:
:
}
public static DataTable DTFromSQL(string Sql, string DBC = null, Int32 timeout = 0)
{
SqlDataAdapter DA = new SqlDataAdapter(Sql, GetDBC(DBC));
DataTable DT = new DataTable();
try
{
DA = new SqlDataAdapter(Sql, DBC);
if (timeout > 0)
{
DA.SelectCommand.CommandTimeout = timeout;
}
DA.Fill(DT);
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
DA.Dispose();
}
return DT;
}
public static DataTable DTFromSQL(string Sql, string DBC = null, Int32 timeout = 30)
{
using (var DA = new SqlDataAdapter(Sql, GetDBC(DBC)))
{
//不可為 null
DataTable DT = new DataTable();
DA.SelectCommand.CommandTimeout = timeout;
DA.Fill(DT);
return DT;
}
}
public static void WriteText1(string FileName, List<string> ltData)
{
foreach(string data in ltData)
{
System.IO.File.AppendAllText(FileName, data);
}
}
public static void WriteText2(string FileName, List<string> ltData)
{
var Res = "";
foreach (string data in ltData)
{
Res += data;
}
System.IO.File.WriteAllText(FileName, Res);
}