将多行值转换为LINQ中的Collection(List) [英] Convert multi-rows values into Collection(List) in LINQ
本文介绍了将多行值转换为LINQ中的Collection(List)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的,
我有以下方法可以带来客户列表
Dears ,
I have the following method that brings list of customers
using (DAL.RptDALDataContext db = new DAL.RptDALDataContext())
{
List<Customer> result = new List<Customer>();
List<DAL.tbl_Customer> collection = db.tbl_Customers.ToList();
foreach (DAL.tbl_Customer item in collection)
{
Customer C1 = new Customer();
C1.CustomerRecordID = item.CustomerRecordID;
C1.FirstName = item.FirstName;
C1.MiddleName = item.MiddleName;
C1.LastName = item.LastName;
C1.Mobile = item.Mobile;
C1.Phone = item.Phone;
C1.Email = item.Email;
C1.OtherInformation = item.OtherInformation;
C1.EmployerName= item.EmployerName;
C1.Nationality = item.tbl_Nationality.Description;
C1.Country = item.tbl_Country.CountryName;
C1.SourceIncome = item.tbl_SourceIncome.Description;
C1.PurposeRemittance = item.tbl_PurposeRemittance.Description;
foreach (DAL.tbl_Address address in item.tbl_Addresses)
{
C1.AddressList = new List<CAddress>();
C1.AddressList.Add(new CAddress() {Street = address.Street, City = address.City,State = address.State });
}
result.Add(C1);
}
return result;
每个客户可以拥有一个以上的地址,该地址可以是多个州,城市或街道
在这种情况下,我在绑定时无法获得网格中的地址值
客户类别包含
each customer can have more than one address that is more than one state,city or street
in this case i don''t get address values in the grid on binding
the customer class contains
public List<CAddress> AddressList
{
get;
set;
}
和地址类
and the address class
public string State
{
get;
set;
}
public string Street
{
get;
set;
}
public string City
{
get;
set;
}
推荐答案
尝试一下.您必须声明
C1.AddressList = new List< caddress>();在foreach循环之前.
try this. You have to declare
C1.AddressList = new List<caddress>(); before foreach loop.
using (DAL.RptDALDataContext db = new DAL.RptDALDataContext())
{
List<customer> result = new List<customer>();
List<dal.tbl_customer> collection = db.tbl_Customers.ToList();
foreach (DAL.tbl_Customer item in collection)
{
Customer C1 = new Customer();
C1.CustomerRecordID = item.CustomerRecordID;
C1.FirstName = item.FirstName;
C1.MiddleName = item.MiddleName;
C1.LastName = item.LastName;
C1.Mobile = item.Mobile;
C1.Phone = item.Phone;
C1.Email = item.Email;
C1.OtherInformation = item.OtherInformation;
C1.EmployerName= item.EmployerName;
C1.Nationality = item.tbl_Nationality.Description;
C1.Country = item.tbl_Country.CountryName;
C1.SourceIncome = item.tbl_SourceIncome.Description;
C1.PurposeRemittance = item.tbl_PurposeRemittance.Description;
C1.AddressList = new List<caddress>();
foreach (DAL.tbl_Address address in item.tbl_Addresses)
{
C1.AddressList.Add(new CAddress() {Street = address.Street, City = address.City,State = address.State });
}
result.Add(C1);
}
return result;
这篇关于将多行值转换为LINQ中的Collection(List)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文