0
從下面的代碼中,我可以使用謂詞搜索並使用類似的方法找到所有商家的Id大於4,我將如何去返回所有商家和憑證的憑證類型ID爲3的憑證可以說。通用列表和Findall謂詞搜索子列表
由於提前
class Program
{
static void Main(string[] args)
{
List<Merchant> merchants = new List<Merchant>(10);
for (int i = 0; i < 10; i++)
{
List<Voucher> vcs = new List<Voucher>();
for (int j = 0; j < 3; j++)
{
vcs.Add(new Voucher(j,"Voucher" + j.ToString(),i, j, "Type_" +j.ToString()));
}
merchants.Add(new Merchant(i, i.ToString() + "_Merchant",vcs));
}
//This will return all the merchant's where the ID is greater than 4
Predicate<Merchant> filterByID;
MerchantFilter filter = new MerchantFilter(4);
filterByID = new Predicate<Merchant>(filter.FilterByMerchantGT4);
List<Merchant> filteredMerchants = merchants.FindAll(filterByID);
}
public class MerchantFilter
{
private int idValue;
public bool FilterByMerchantGT4(Merchant merch)
{
return merch.MerchantID > idValue;
}
public MerchantFilter(int value)
{
idValue = value;
}
}
public class Merchant
{
public int MerchantID { get; set; }
public string MerchantName { get; set; }
public List<Voucher> MerchantVouchers { get; set; }
public Merchant(int id, string Name, List<Voucher> vouchers)
{
MerchantID = id;
MerchantName = Name;
MerchantVouchers = vouchers;
}
}
public class Voucher
{
public int VoucherID { get; set; }
public string VoucherName { get; set; }
public int MerchantVoucherID { get; set; }
public int VoucherTypeID { get; set; }
public string VoucherTypeName { get; set; }
public Voucher(int ID, string Name, int merchantID, int typeID, string TypeName)
{
VoucherID = ID;
VoucherName = Name;
MerchantVoucherID = merchantID;
VoucherTypeID = typeID;
VoucherTypeName = TypeName;
}
}
感謝您的幫助。解決了我的問題。 – Paul 2011-03-29 11:52:30