-1

我在下面有这个查询。之后,我想用结果填充一个列表。我应该怎么做 ?

var query = from cust in context.Customer
            join city in context.Cities on cust.id_city equals city.id
            join state in context.State on city.id_state equals state.id
            join reg in context.Region on state.id_region equals reg.id
            select new 
            {
               nameCust = cust.name,
               nameCity = city.name,
               nameState = state.name,
               nameRegion = reg.name
            };
4

1 回答 1

1

您应该能够调用ToList()返回值:

var queryResult = (from cust in context.Customer
            join city in context.Cities on cust.id_city equals city.id
            join state in context.State on city.id_state equals state.id
            join reg in context.Region on state.id_region equals reg.id
            select new 
            {
               nameCust = cust.name,
               nameCity = city.name,
               nameState = state.name,
               nameRegion = reg.name
            }).ToList();

我想知道,如果您使用的是实体框架。如果是这样:这可能是一个更好的解决方案:

var queryResult = context.Customer
    .Include(customer => customer.City)
    .Include(customer => customer.State)
    .Include(customer => customer.Region)
    .ToList();

( ! )请记住,您正在使用将完整的表加载到内存中ToList()

于 2013-11-10T22:03:06.717 回答