我必须将两种不同类型的数组连接起来以形成一个新数组。
让我们看看应该在其中创建条件的类。
public class MutualFundParameter
{
public int Id { get; set; }
public string AcNo { get; set; }
public string CustomerName { get; set; }
}
public class VoucherDetails
{
public int Id { get; set; }
public int AcNo { get; set; }
public decimal Amount { get; set; }
public int MasterId { get; set; }
}
public class VoucherMaster
{
public int Id { get; set; }
public DateTime DateTime { get; set; }
public ICollection<VoucherDetails> VoucherDetails { get; set; }
}
让我们将一些数据添加到该对象中
// mutual fund parameter
var mutualFundParameters = new List<MutualFundParameter>
{
new MutualFundParameter { Id = 1, AcNo = "001", CustomerName = "Ram" },
new MutualFundParameter { Id = 2, AcNo = "002", CustomerName = "Shyam" },
new MutualFundParameter { Id = 3, AcNo = "003", CustomerName = "Hari" },
new MutualFundParameter { Id = 4, AcNo = "004", CustomerName = "Gita" },
new MutualFundParameter { Id = 5, AcNo = "005", CustomerName = "Sita" },
};
// voucher details
var voucherDetails = new List<VoucherDetails>
{
new VoucherDetails { Id = 1, AcNo = 001, Amount = 10.00M, MasterId = 1 },
new VoucherDetails { Id = 2, AcNo = 001, Amount = 120.00M, MasterId = 1 },
new VoucherDetails { Id = 3, AcNo = 002, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 4, AcNo = 002, Amount = 120.00M, MasterId = 1 },
new VoucherDetails { Id = 5, AcNo = 002, Amount = 30.00M, MasterId = 1 },
new VoucherDetails { Id = 6, AcNo = 002, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 7, AcNo = 003, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 8, AcNo = 003, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 9, AcNo = 003, Amount = 20.00M, MasterId = 1 },
new VoucherDetails { Id = 10, AcNo = 004, Amount = 10.00M, MasterId = 2 },
new VoucherDetails { Id = 11, AcNo = 005, Amount = 20.00M, MasterId = 2 },
new VoucherDetails { Id = 12, AcNo = 006, Amount = 30.00M, MasterId = 2 },
new VoucherDetails { Id = 13, AcNo = 007, Amount = 40.00M, MasterId = 2 },
new VoucherDetails { Id = 14, AcNo = 008, Amount = 50.00M, MasterId = 2 },
new VoucherDetails { Id = 15, AcNo = 009, Amount = 60.00M, MasterId = 2 },
new VoucherDetails { Id = 16, AcNo = 001, Amount = 70.00M, MasterId = 2 },
};
// voucher
var voucherMasters = new List<VoucherMaster>
{
new VoucherMaster
{
Id = 1,
VoucherDetails = voucherDetails
.Where(x => x.MasterId == 1)
.ToList(),
DateTime = DateTime.Now }
};
现在我需要将MutualFundParameter
与VoucherDetails一起加入,但这在“ Voucher Master”列表中
要加入,我已经尝试过了,但这不能按预期工作
var res = voucherMasters.Join(
mutualFundParameters,
voucher => voucher.VoucherDetails.FirstOrDefault().AcNo,
mutual => mutual.AcNo,
(voucher, mutual) => new
{
}
);
感谢您的积极回应。
答案 0 :(得分:0)
假设interFundParameter和voucherDetail中的AcNo是同一类型,这是查询内存中集合或db的两个示例: (注意:我尚未编译/运行代码,因此请原谅任何语法错误)
var resultFromInMemory = (
from vd in voucherMasters.SelectMany(x => x.voucherDetails)
join mfp in mutualFundParameters on mfp.AcNo equals vd.AcNo
select new { vd, mfp });
var resultFromDb = (
from vd in db.voucherDetails
join mfp in db.mutualFundParameters on mfp.AcNo equals vd.AcNo
select new { vd, mfp });
对于内存中集合,因为您希望voucherDetails是voucherMaster的子集合,所以在commonFundParameters和voucherDetails之间执行联接需要首先将voucherDetails合并到一个临时的单个集合中,SelectMany可以帮助
如果是数据库(假设是关系数据库),则不需要这种中间集合,因为每个实体都是其中的平面表。