From the code below I am able to use a predicate search and find all the merchants that have an Id greater than 4, using a similar approach how would I go about returning all the merchants and their vouchers where the Voucher had a VoucherTypeID of 3 lets say.
从下面的代码中,我可以使用谓词搜索并找到所有Id大于4的商家,使用类似的方法,我如何返回所有商家及其凭证,其中凭证的VoucherTypeID为3。
Thanks in Advance
谢谢提前
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;
}
}
1 个解决方案
#1
1
Using linq, you could do this:
使用linq,您可以这样做:
var query = from merchant in merchants
let filteredVouchers = from voucher in merchant.MerchantVouchers
where voucher.VoucherTypeID == 3
select voucher
select new { merchant, filteredVouchers };
or, equivalently, this:
,或等价于:
var query =
merchants.Select(
merchant =>
new
{
merchant,
filteredVouchers = merchant.MerchantVouchers.Where(voucher => voucher.VoucherTypeID == 3)
})
.Select(t => new { t.merchant, t.filteredVouchers });
or, avoiding linq:
或者,避免linq:
foreach(var merchant in merchants)
{
var filteredVouchers = merchant.MerchantVouchers.FindAll(v => v.VoucherTypeID == 3);
//...some useful code here
}
Notice how you don't need to define the MerchantFilter class. You can use a lambda expression to define the argument to the FindAll method.
注意,您不需要定义商船过滤器类。您可以使用lambda表达式来定义FindAll方法的参数。
#1
1
Using linq, you could do this:
使用linq,您可以这样做:
var query = from merchant in merchants
let filteredVouchers = from voucher in merchant.MerchantVouchers
where voucher.VoucherTypeID == 3
select voucher
select new { merchant, filteredVouchers };
or, equivalently, this:
,或等价于:
var query =
merchants.Select(
merchant =>
new
{
merchant,
filteredVouchers = merchant.MerchantVouchers.Where(voucher => voucher.VoucherTypeID == 3)
})
.Select(t => new { t.merchant, t.filteredVouchers });
or, avoiding linq:
或者,避免linq:
foreach(var merchant in merchants)
{
var filteredVouchers = merchant.MerchantVouchers.FindAll(v => v.VoucherTypeID == 3);
//...some useful code here
}
Notice how you don't need to define the MerchantFilter class. You can use a lambda expression to define the argument to the FindAll method.
注意,您不需要定义商船过滤器类。您可以使用lambda表达式来定义FindAll方法的参数。