星期三, 4月 29, 2009

(題目)抓取陣列中最接近且小於輸入值的數

今天有人問我這個問題"抓取陣列中最接近且小於輸入值的數"..我記錄下來..以後有需要的人可以參考一下..

public static class CompareIntHelper
{
//過濾比自己小的int[]
private static int[] GetThenLessArray(int number, int[] randomArr)
{
return randomArr.Where(a => a < number).ToArray();
}
//抓最接近
public static int GetThenLessAndClosedNumber(int number, int[] randomArr)
{
randomArr = GetThenLessArray(number, randomArr);
if (randomArr.Length == 0)
throw new Exception("沒有數字比亂數陣列小");

int ans = randomArr[0];
int intTemp = int.MaxValue;
Array.ForEach(randomArr, a =>
{
if (number - a < intTemp)
{
ans = a;
intTemp = number - a;
}
});
return ans;
}
}

使用如下..

//測試資料
Random r = new Random();
int[] randomArr = new int[1000];
for (int i = 0; i < 1000; i++)
randomArr[i] = r.Next(0, 10000);
//列出結果
try
{
Response.Write(CompareIntHelper.GetThenLessAndClosedNumber(1200, randomArr) + "<br />");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

--2009/5/1 更新--
經過一些討論..發現只要加入排序..程式就更簡單了..Tongue out

public static class CompareIntHelper1
{
//抓最接近,且小於number
public static int GetThenLessAndClosedNumber(int number, int[] randomArr)
{
var result = randomArr.OrderByDescending(a => a).Where(a => number > a);
if (result.Count() == 0)
throw new Exception("沒有數字比亂數陣列小");
return result.First();
}
}

星期四, 4月 23, 2009

Entity Framework 處理關聯資料表

有人問我..如果一堆關聯的 table..想用 Entity Framework 去抓取資料..會不會很麻煩..我做個簡單的範例..各位看倌看看ㄅ..

先準備 table..

ddc

再來..我希望..以 TableA 的 ID 來作為搜尋條件..找出 TableD 的資料..

using (NORTHWNDModel.NORTHWNDEntities db = new NORTHWNDModel.NORTHWNDEntities())
{
var aaaa = db.TableD.Where(a => a.TableC.TableB.TableA.ID == 1);
GridView1.DataSource = aaaa;
GridView1.DataBind();
}

很簡單ㄅ..如果用 Entity SQL 可以這樣做..

using (NORTHWNDModel.NORTHWNDEntities db = new NORTHWNDModel.NORTHWNDEntities())
{
var aaaa = db.CreateQuery<NORTHWNDModel.TableD>(
"select value d from TableD as d where d.TableC.TableB.TableA.ID = 1");
GridView1.DataSource = aaaa;
GridView1.DataBind();
}

我們看一下 EntityFramework 幫我們產生出來的 TSQL..

dfd

這樣就可以很清楚知道..用 Entity Framework 去處理關聯 Table 是蠻直覺又輕鬆的..在問題在一起討論ㄅ..
Big GrinBig GrinBig GrinBig GrinBig GrinBig GrinBig GrinBig GrinBig GrinBig GrinBig Grin..


星期三, 4月 15, 2009

抓出Entity Framework的SQL指令

最近在研究Entity Framework..發現EF真的蠻不錯用的..尤其在Linq熟得時候..EF就可以很得心應手..今天在寫的時候..覺得EF丟出的指令還是要試著去了解..因為有時複雜的Linq to Entity還是需要看看怎樣寫才能丟出有效率的SQL Statement..下面就是簡單的兩個方法去抓出SQL Statement..

第一種方法

using (NORTHWNDModel.NORTHWNDEntities db = new NORTHWNDModel.NORTHWNDEntities())
{
var aaaa = db.Orders.Select(a => new { a.ShipName, a.ShipCity }).Take(10);
ObjectQuery oq = aaaa as ObjectQuery;
if (oq != null)
{
string SQLStr = oq.ToTraceString();
Response.Write(SQLStr);
}
}

第二種方法

using (NORTHWNDModel.NORTHWNDEntities db = new NORTHWNDModel.NORTHWNDEntities())
{
var aaaa = db.Orders.Select(a => new { a.ShipName, a.ShipCity }).Take(10);
MethodInfo mi = aaaa.GetType().GetMethod("ToTraceString");
if (mi != null)
Response.Write(mi.Invoke(aaaa, null).ToString());
}

結果:

fdf

第一種方法效率好很多..如果可以就用第一種方法ㄅ..Open-mouthedOpen-mouthedOpen-mouthedOpen-mouthedOpen-mouthedOpen-mouthedOpen-mouthed..

參考:

星期三, 4月 01, 2009

(題目)如何去抓出雙數周的週六

今天看到一個題目"如何去抓出雙數周,用來判斷隔週休"..隨手寫了程式..紀錄一下..Tongue out..

這樣執行就可以列出第雙數週週六了

WeekSpProvider wp = new WeekSpProvider(2009, 4, DayOfWeek.Saturday, 12);
var data = wp.GetDateTimeList(2);
foreach (var a in data)
Response.Write(a.Date.ToString("yyyy/MM/dd")+"<br />");

class:

/// <summary>
/// 來處理數個月的資料
/// </summary>
public class WeekSpProvider
{
List<DateObj> dateTimeList = new List<DateObj>();
public WeekSpProvider(int startYear, int startMonth, DayOfWeek dayWeek, int monthCount)
{
DateTime dtStart = new DateTime(startYear, startMonth, 1);
for (int i = 1; i <= monthCount; i++)
{
MonthObj mo = new MonthObj(dtStart, dayWeek);
this.dateTimeList = this.dateTimeList
.Union(mo.GetDateTimeList()).ToList();
dtStart = dtStart.AddMonths(1);
}
}
/// <summary>
/// 找到倍數的週數
/// </summary>
/// <param name="multiplier"></param>
/// <returns></returns>
public List<DateObj> GetDateTimeList(int multiplier)
{
return this.dateTimeList.Where(a => a.Number % multiplier == 0).ToList();
}
}
/// <summary>
/// 處理一個月的月份
/// </summary>
public class MonthObj
{
int year;
int month;
List<DateObj> dateTimeList = new List<DateObj>();
DateTime nextMonth;
DayOfWeek dayWeek;
public MonthObj(DateTime dt, DayOfWeek dayWeek)
{
this.year = dt.Year;
this.month = dt.Month;
this.dayWeek = dayWeek;
//抓出這個月第一天,跟下個月的第一天
DateTime dateTime = new DateTime(year, month, 1);
nextMonth = dateTime.AddMonths(1);
//迴圈去取得每一周
int i = 0;
do
{
if (IsYouWantDay(dateTime))
{
AddDate(++i, dateTime);
dateTime = dateTime.AddDays(7);
}
else
{
dateTime = dateTime.AddDays(1);
}
}
while (dateTime < nextMonth);
}
public List<DateObj> GetDateTimeList()
{
return dateTimeList;
}
private void AddDate(int number, DateTime dt)
{
if (dt < nextMonth)
this.dateTimeList.Add(
new DateObj() { Number = number, Month = dt.Month, Date = dt, DayWeek = dayWeek });
}
private bool IsYouWantDay(DateTime dt)
{
return dt.DayOfWeek == dayWeek;
}
}
/// <summary>
/// 來紀錄日期的資料
/// </summary>
public struct DateObj
{
public int Number { get; set; }
public int Month { get; set; }
public DayOfWeek DayWeek { get; set; }
public DateTime Date { get; set; }
}

這class還可以作到類似下列的效果..

ghj

試試看ㄅ..蠻不錯玩的..Nerd..