為了方面大家更好地理解我寫這篇文章的目的,這篇文章我會詳細介紹我的代碼,我有什麼理解錯誤的,請大家不吝賜教。
首先我定義2 個類。
公司類:
UserCompany.cs
class UserCompany
{
public int ID { get; set; }
public string CompanyName { get; set; }
public string CompanyDesc { get; set; }
}
員工類:
UserInfo.cs
class UserInfo
{
public int ID { get; set; }
public string UserName { get; set; }
public int UserCompanyID { get; set; }
public UserCompany UserCompany_UserCompanyID { get; set; }
}
也許大家看到了,這裡的員工類(UserInfo)有一個屬性UserCompanyID 來標記這個員工屬於哪個公司。
同時也有一個屬性UserCompany_UserCompanyID 表示這個UserCompanyID 對應的公司類(UserCompany)。
下面是一個方法來得到所有的公司,一般我們的數據是保存在數據庫中的。為了演示,這裡就不訪問數據庫了。
/// <summary>
/// 得到所有的公司信息
/// </summary>
/// <returns></returns>
static IList<UserCompany> GetAllUserCompanys()
{
return new List<UserCompany>()
{
new UserCompany(){ ID = 1, CompanyName = "新浪", CompanyDesc = "新浪是個大公司" },
new UserCompany(){ ID = 2, CompanyName = "百度", CompanyDesc = "百度是個大公司" },
new UserCompany(){ ID = 3, CompanyName = "騰訊", CompanyDesc = "騰訊是個大公司" },
new UserCompany(){ ID = 4, CompanyName = "網易", CompanyDesc = "網易是個大公司" },
new UserCompany(){ ID = 5, CompanyName = "搜狐", CompanyDesc = "搜狐是個大公司" },
new UserCompany(){ ID = 6, CompanyName = "阿裡巴巴", CompanyDesc = "阿裡巴巴是個大公司" }
};
}
同時也有一個方法來得到所有的員工。
/// <summary>
/// 得到所有的員工信息
/// </summary>
/// <returns></returns>
static IList<UserInfo> GetAllUserInfos()
{
return new List<UserInfo>()
{
new UserInfo(){ ID = 1, UserName = "張三", UserCompanyID = 2 },
new UserInfo(){ ID = 2, UserName = "李四", UserCompanyID = 1 },
new UserInfo(){ ID = 3, UserName = "王五", UserCompanyID = 4 },
new UserInfo(){ ID = 4, UserName = "趙六", UserCompanyID = 1 },
new UserInfo(){ ID = 5, UserName = "楊七", UserCompanyID = 3 },
new UserInfo(){ ID = 6, UserName = "朱八", UserCompanyID = 1 },
new UserInfo(){ ID = 7, UserName = "孫九", UserCompanyID = 2 },
new UserInfo(){ ID = 8, UserName = "蕭十一郎", UserCompanyID = 1 }
};
}
一般如果我們得到了所有的員工的基本信息後,還想得到這些員工對應的公司信息,比如:公司名、公司描述等等。
怎麼辦呢?我的思路是,當我們得到這些員工的基本信息後,就能夠得到每個員工對應的公司ID(UserCompanyID),
然後我在利用Linq 取出這些公司ID(UserCompanyID),保存到一個可以枚舉的集合中IEnumerable<int>,然後去
數據庫中取出這些公司ID(UserCompanyID)對應的公司信息。下面是代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConAppTest
{
class Program
{
static void Main(string[] args)
{
IList<UserInfo> users = GetAllUserInfos();
IEnumerable<int> userCompanyIds = users.Select<UserInfo, int>(u => u.UserCompanyID).Distinct();
IEnumerable<UserCompany> existsCompanys = GetCompanysByCompanyIds(userCompanyIds);
foreach (UserCompany companyItem in existsCompanys)
{
if(companyItem == null)
{
continue;
}
Console.WriteLine(companyItem.CompanyName);
}
}
/// <summary>
/// 得到所有的員工信息
/// </summary>
/// <returns></returns>
static IList<UserInfo> GetAllUserInfos()
{
return new List<UserInfo>()
{
new UserInfo(){ ID = 1, UserName = "張三", UserCompanyID = 2 },
new UserInfo(){ ID = 2, UserName = "李四", UserCompanyID = 1 },
new UserInfo(){ ID = 3, UserName = "王五", UserCompanyID = 4 },
new UserInfo(){ ID = 4, UserName = "趙六", UserCompanyID = 1 },
new UserInfo(){ ID = 5, UserName = "楊七", UserCompanyID = 3 },
new UserInfo(){ ID = 6, UserName = "朱八", UserCompanyID = 1 },
new UserInfo(){ ID = 7, UserName = "孫九", UserCompanyID = 2 },
new UserInfo(){ ID = 8, UserName = "蕭十一郎", UserCompanyID = 1 }
};
}
/// <summary>
/// 得到所有的公司信息
/// </summary>
/// <returns></returns>
static IList<UserCompany> GetAllUserCompanys()
{
return new List<UserCompany>()
{
new UserCompany(){ ID = 1, CompanyName = "新浪", CompanyDesc = "新浪是個大公司" },
new UserCompany(){ ID = 2, CompanyName = "百度", CompanyDesc = "百度是個大公司" },
new UserCompany(){ ID = 3, CompanyName = "騰訊", CompanyDesc = "騰訊是個大公司" },
new UserCompany(){ ID = 4, CompanyName = "網易", CompanyDesc = "網易是個大公司" },
new UserCompany(){ ID = 5, CompanyName = "搜狐", CompanyDesc = "搜狐是個大公司" },
new UserCompany(){ ID = 6, CompanyName = "阿裡巴巴", CompanyDesc = "阿裡巴巴是個大公司" }
};
}
/// <summary>
/// 根據一個或多個CompanyId 得到公司。
/// </summary>
/// <param name="companyIds">一個或多個CompanyId</param>
/// <returns></returns>
static IEnumerable<UserCompany> GetCompanysByCompanyIds(IEnumerable<int> companyIds)
{
IList<UserCompany> companys = GetAllUserCompanys();
foreach (int companyId in companyIds)
{
yield return companys.FirstOrDefault(c => c.ID == companyId);
}
}
}
}
運行結果截圖:
如果我們要讓這些員工類(UserInfo)的實體屬性UserCompany_UserCompanyID 自動指向已經被加載出來的公司實體,怎麼辦呢?下面有一種方法(完整代碼):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConAppTest
{
class Program
{
static void Main(string[] args)
{
IList<UserInfo> users = GetAllUserInfos();
IEnumerable<int> userCompanyIds = users.Select<UserInfo, int>(u => u.UserCompanyID).Distinct();
IEnumerable<UserCompany> existsCompanys = GetCompanysByCompanyIds(userCompanyIds);
foreach (UserCompany companyItem in existsCompanys)
{
if(companyItem == null)
{
continue;
}
Console.WriteLine(companyItem.CompanyName);
}
// 把一個實體類作為屬性的引用指向另一個實體類
foreach (UserInfo userItem in users)
{
foreach (UserCompany companyItem in existsCompanys)
{
if (userItem.UserCompanyID == companyItem.ID)
{
userItem.UserCompany_UserCompanyID = companyItem;
break;
}
}
}
// 下面是打印出信息
foreach (UserInfo userItem in users)
{
Console.WriteLine("姓名:" + userItem.UserName + ",公司:" + userItem.UserCompany_UserCompanyID.CompanyName);
}
}
/// <summary>
/// 得到所有的員工信息
/// </summary>
/// <returns></returns>
static IList<UserInfo> GetAllUserInfos()
{
return new List<UserInfo>()
{
new UserInfo(){ ID = 1, UserName = "張三", UserCompanyID = 2 },
new UserInfo(){ ID = 2, UserName = "李四", UserCompanyID = 1 },
new UserInfo(){ ID = 3, UserName = "王五", UserCompanyID = 4 },
new UserInfo(){ ID = 4, UserName = "趙六", UserCompanyID = 1 },
new UserInfo(){ ID = 5, UserName = "楊七", UserCompanyID = 3 },
new UserInfo(){ ID = 6, UserName = "朱八", UserCompanyID = 1 },
new UserInfo(){ ID = 7, UserName = "孫九", UserCompanyID = 2 },
new UserInfo(){ ID = 8, UserName = "蕭十一郎", UserCompanyID = 1 }
};
}
/// <summary>
/// 得到所有的公司信息
/// </summary>
/// <returns></returns>
static IList<UserCompany> GetAllUserCompanys()
{
return new List<UserCompany>()
{
new UserCompany(){ ID = 1, CompanyName = "新浪", CompanyDesc = "新浪是個大公司" },
new UserCompany(){ ID = 2, CompanyName = "百度", CompanyDesc = "百度是個大公司" },
new UserCompany(){ ID = 3, CompanyName = "騰訊", CompanyDesc = "騰訊是個大公司" },
new UserCompany(){ ID = 4, CompanyName = "網易", CompanyDesc = "網易是個大公司" },
new UserCompany(){ ID = 5, CompanyName = "搜狐", CompanyDesc = "搜狐是個大公司" },
new UserCompany(){ ID = 6, CompanyName = "阿裡巴巴", CompanyDesc = "阿裡巴巴是個大公司" }
};
}
/// <summary>
/// 根據一個或多個CompanyId 得到公司。
/// </summary>
/// <param name="companyIds">一個或多個CompanyId</param>
/// <returns></returns>
static IEnumerable<UserCompany> GetCompanysByCompanyIds(IEnumerable<int> companyIds)
{
IList<UserCompany> companys = GetAllUserCompanys();
foreach (int companyId in companyIds)
{
yield return companys.FirstOrDefault(c => c.ID == companyId);
}
}
}
class UserCompany
{
public int ID { get; set; }
public string CompanyName { get; set; }
public string CompanyDesc { get; set; }
}
class UserInfo
{
public int ID { get; set; }
public string UserName { get; set; }
public int UserCompanyID { get; set; }
public UserCompany UserCompany_UserCompanyID { get; set; }
}
}
運行結果截圖:
不知道大家注意下面這段代碼沒有:
foreach (UserInfo userItem in users)
{
foreach (UserCompany companyItem in existsCompanys)
{
if (userItem.UserCompanyID == companyItem.ID)
{
userItem.UserCompany_UserCompanyID = companyItem;
break;
}
}
}
如果我們的很多地方都要實現“把一個實體類作為屬性的引用指向另一個實體類”,怎麼辦呢?難道也是2 層循環,先判斷ID 是否相等,然後賦值嗎?
我不喜歡寫重復的代碼,於是想到了委托來簡化。下面是代碼:
/// <summary>
/// 讓指針指向一個實體
/// </summary>
/// <typeparam name="TSource">源實體集合</typeparam>
/// <typeparam name="TValue">值實體集合</typeparam>
/// <param name="sourceItems">源實體集合</param>
/// <param name="sourceValueItems">值實體集合</param>
/// <param name="fn">判斷true or false 的委托</param>
/// <param name="actionEvaluate">做一個什麼事的委托</param>
static void LetPointerEvaluateEntity<TSource, TValue>(IEnumerable<TSource> sourceItems,
IEnumerable<TValue> sourceValueItems,
Func<TSource, TValue, bool> fn,
Action<TSource, TValue> actionEvaluate)
{
foreach (var sourceItem in sourceItems)
{
foreach (var valueItem in sourceValueItems)
{
if (fn(sourceItem, valueItem))
{
//做一些事,比如:賦值(把一個對象的屬性指向另一個對象)
actionEvaluate(sourceItem, valueItem);
break;
}
}
}
}
最後在客戶端調用就可以了。完整代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConAppTest
{
class Program
{
static void Main(string[] args)
{
IList<UserInfo> users = GetAllUserInfos();
IEnumerable<int> userCompanyIds = users.Select<UserInfo, int>(u => u.UserCompanyID).Distinct();
IEnumerable<UserCompany> existsCompanys = GetCompanysByCompanyIds(userCompanyIds);
foreach (UserCompany companyItem in existsCompanys)
{
if(companyItem == null)
{
continue;
}
Console.WriteLine(companyItem.CompanyName);
}
// 把一個實體類作為屬性的引用指向另一個實體類
/*
foreach (UserInfo userItem in users)
{
foreach (UserCompany companyItem in existsCompanys)
{
if (userItem.UserCompanyID == companyItem.ID)
{
userItem.UserCompany_UserCompanyID = companyItem;
break;
}
}
}
*/
// 把一個實體類作為屬性的引用指向另一個實體類(用委托來簡化後)
LetPointerEvaluateEntity<UserInfo, UserCompany>(users, existsCompanys,
(user, userCompany) => user.UserCompanyID == userCompany.ID,
(user, userCompany) => user.UserCompany_UserCompanyID = userCompany);
// 下面是打印出信息
foreach (UserInfo userItem in users)
{
Console.WriteLine("姓名:" + userItem.UserName + ",公司:" + userItem.UserCompany_UserCompanyID.CompanyName);
}
}
/// <summary>
/// 得到所有的員工信息
/// </summary>
/// <returns></returns>
static IList<UserInfo> GetAllUserInfos()
{
return new List<UserInfo>()
{
new UserInfo(){ ID = 1, UserName = "張三", UserCompanyID = 2 },
new UserInfo(){ ID = 2, UserName = "李四", UserCompanyID = 1 },
new UserInfo(){ ID = 3, UserName = "王五", UserCompanyID = 4 },
new UserInfo(){ ID = 4, UserName = "趙六", UserCompanyID = 1 },
new UserInfo(){ ID = 5, UserName = "楊七", UserCompanyID = 3 },
new UserInfo(){ ID = 6, UserName = "朱八", UserCompanyID = 1 },
new UserInfo(){ ID = 7, UserName = "孫九", UserCompanyID = 2 },
new UserInfo(){ ID = 8, UserName = "蕭十一郎", UserCompanyID = 1 }
};
}
/// <summary>
/// 得到所有的公司信息
/// </summary>
/// <returns></returns>
static IList<UserCompany> GetAllUserCompanys()
{
return new List<UserCompany>()
{
new UserCompany(){ ID = 1, CompanyName = "新浪", CompanyDesc = "新浪是個大公司" },
new UserCompany(){ ID = 2, CompanyName = "百度", CompanyDesc = "百度是個大公司" },
new UserCompany(){ ID = 3, CompanyName = "騰訊", CompanyDesc = "騰訊是個大公司" },
new UserCompany(){ ID = 4, CompanyName = "網易", CompanyDesc = "網易是個大公司" },
new UserCompany(){ ID = 5, CompanyName = "搜狐", CompanyDesc = "搜狐是個大公司" },
new UserCompany(){ ID = 6, CompanyName = "阿裡巴巴", CompanyDesc = "阿裡巴巴是個大公司" }
};
}
/// <summary>
/// 根據一個或多個CompanyId 得到公司。
/// </summary>
/// <param name="companyIds">一個或多個CompanyId</param>
/// <returns></returns>
static IEnumerable<UserCompany> GetCompanysByCompanyIds(IEnumerable<int> companyIds)
{
IList<UserCompany> companys = GetAllUserCompanys();
foreach (int companyId in companyIds)
{
yield return companys.FirstOrDefault(c => c.ID == companyId);
}
}
/// <summary>
/// 讓指針指向一個實體
/// </summary>
/// <typeparam name="TSource">源實體集合</typeparam>
/// <typeparam name="TValue">值實體集合</typeparam>
/// <param name="sourceItems">源實體集合</param>
/// <param name="sourceValueItems">值實體集合</param>
/// <param name="fn">判斷true or false 的委托</param>
/// <param name="actionEvaluate">做一個什麼事的委托</param>
static void LetPointerEvaluateEntity<TSource, TValue>(IEnumerable<TSource> sourceItems,
IEnumerable<TValue> sourceValueItems,
Func<TSource, TValue, bool> fn,
Action<TSource, TValue> actionEvaluate)
{
foreach (var sourceItem in sourceItems)
{
foreach (var valueItem in sourceValueItems)
{
if (fn(sourceItem, valueItem))
{
//做一些事,比如:賦值(把一個對象的屬性指向另一個對象)
actionEvaluate(sourceItem, valueItem);
break;
}
}
}
}
}
class UserCompany
{
public int ID { get; set; }
public string CompanyName { get; set; }
public string CompanyDesc { get; set; }
}
class UserInfo
{
public int ID { get; set; }
public string UserName { get; set; }
public int UserCompanyID { get; set; }
public UserCompany UserCompany_UserCompanyID { get; set; }
}
}
運行結果和上面的代碼一樣,這個方法讓代碼簡化了很多,是不是呢?您有什麼不同的觀點,請分享!下面是截圖:
呵呵,這是我第一次發首頁,謝謝你來捧場,也許你可能覺得這代碼太簡單了,不值得發首頁。但無論如何,和大家分享自己的一點技術應該沒有錯吧,
也許還是可能幫助其他人提高一點點,謝謝!
代碼下載:http://www.BkJia.com/uploadfile/2011/1008/20111008034508513.rar