我在格式化linq查询的结果时遇到问题
var listOfCustomerSearchResult = (from customer in entities.Customers
where customer.Number.StartsWith(customerNumber)
select new CustomerSearchResult
{
AccountNbr = (customer.Number.IndexOf(".") > 0) ? customer.Number.Substring(0, customer.Number.IndexOf(".")) : customer.Number,
SubAccountNbr = (customer.Number.IndexOf(".") > 0) ? customer.Number.Substring(customer.Number.IndexOf(".") + 1) : string.Empty,
FirstAndLastName = (customer.Contact.IsIndividual) ? (customer.Contact.FirstNameCareOf + " " ?? string.Empty) + (customer.Contact.Name ?? string.Empty) : (customer.Contact.Name ?? string.Empty) + " " + (customer.Contact.FirstNameCareOf ?? string.Empty),
StreetAddress = customer.Contact.Addresses.FirstOrDefault().StreetAddress ?? string.Empty,
City = customer.Contact.Addresses.FirstOrDefault().City ?? string.Empty,
ZipCode = customer.Contact.Addresses.FirstOrDefault().ZipCode ?? string.Empty,
Region = customer.Contact.Addresses.FirstOrDefault().Region.Code ?? string.Empty,
Delivery = string.Empty,
IsActive = customer.IsActive,
IsAdministrative = customer.IsAdministrative,
SearchStep = 1,
CustomerId = customer.Id,
AccountType = customer.Type.EnumId,
Phone = customer.Contact.Phones.FirstOrDefault().Number ?? string.Empty
}).Take(500).ToList();
1-我需要格式化地址,电话等但我不能直接在Select new中完成...有没有办法打电话给我的"助手功能" (如FormatPhoneNumber(),FormatAddress(),...)没有扫描所有结果并在执行查询后逐个格式化每个结果?例如:像MyQuery ... blabla ... Take(500)。ToList()。ImaginaryFormatProperties(x => x.Phone = FormatPhone(x.Phone),x.Address = FormatAddress(x.Address)...
2-我需要多次调用此查询,每次使用大量不同的WHERE子句。有没有办法做到这一点,而不必创建CustomerSearchResult并每次分配这样的每个属性???我不希望每次都重复此设置部分,因为它没有改变
谢谢!
答案 0 :(得分:1)
1)。 您可以向CustomerSearchResult模型类添加readonly属性,以便为您进行格式化。
public class CustomerSearchResult
{
//All of your current properties in this class here
//New readonly Property
public string PhoneFormatedString
{
get
{
return //Do your formatting here using the Phone property, or pass it to a function
}
}
}
2。)你想创建一个像这样的IQueryable结果。
public IQueryable<CustomerSearchResult> CustomerSearchBaseQuery()
{
IQueryable<CustomerSearchResult> listOfCustomerSearchResult = (from customer in entities.Customers
select new CustomerSearchResult
{
AccountNbr = (customer.Number.IndexOf(".") > 0) ? customer.Number.Substring(0, customer.Number.IndexOf(".")) : customer.Number,
SubAccountNbr = (customer.Number.IndexOf(".") > 0) ? customer.Number.Substring(customer.Number.IndexOf(".") + 1) : string.Empty,
FirstAndLastName = (customer.Contact.IsIndividual) ? (customer.Contact.FirstNameCareOf + " " ?? string.Empty) + (customer.Contact.Name ?? string.Empty) : (customer.Contact.Name ?? string.Empty) + " " + (customer.Contact.FirstNameCareOf ?? string.Empty),
StreetAddress = customer.Contact.Addresses.FirstOrDefault().StreetAddress ?? string.Empty,
City = customer.Contact.Addresses.FirstOrDefault().City ?? string.Empty,
ZipCode = customer.Contact.Addresses.FirstOrDefault().ZipCode ?? string.Empty,
Region = customer.Contact.Addresses.FirstOrDefault().Region.Code ?? string.Empty,
Delivery = string.Empty,
IsActive = customer.IsActive,
IsAdministrative = customer.IsAdministrative,
SearchStep = 1,
CustomerId = customer.Id,
AccountType = customer.Type.EnumId,
Phone = customer.Contact.Phones.FirstOrDefault().Number ?? string.Empty
});
return listOfCustomerSearchResult;
}
然后你可以从你的IQueryable查询。在调用ToList()
之前,不会执行Sql public List<CustomerSearchResult> CustomerSearchByNumber(string customerNumber)
{
return CustomerSearchBaseQuery().Where(x => x.AccountNbr.StartsWith(customerNumber)).ToList();
}
编辑1:根据您的评论尝试使用Func
进行操作//Customer here is the class from your entity model
public static Expression<Func<Customer, CustomerSearchResult>> customerSelector = (customer) =>
new CustomerSearchResult
{
AccountNbr = (customer.Number.IndexOf(".") > 0) ? customer.Number.Substring(0, customer.Number.IndexOf(".")) : customer.Number,
SubAccountNbr = (customer.Number.IndexOf(".") > 0) ? customer.Number.Substring(customer.Number.IndexOf(".") + 1) : string.Empty,
FirstAndLastName = (customer.Contact.IsIndividual) ? (customer.Contact.FirstNameCareOf + " " ?? string.Empty) + (customer.Contact.Name ?? string.Empty) : (customer.Contact.Name ?? string.Empty) + " " + (customer.Contact.FirstNameCareOf ?? string.Empty),
StreetAddress = customer.Contact.Addresses.FirstOrDefault().StreetAddress ?? string.Empty,
City = customer.Contact.Addresses.FirstOrDefault().City ?? string.Empty,
ZipCode = customer.Contact.Addresses.FirstOrDefault().ZipCode ?? string.Empty,
Region = customer.Contact.Addresses.FirstOrDefault().Region.Code ?? string.Empty,
Delivery = string.Empty,
IsActive = customer.IsActive,
IsAdministrative = customer.IsAdministrative,
SearchStep = 1,
CustomerId = customer.Id,
AccountType = customer.Type.EnumId,
Phone = customer.Contact.Phones.FirstOrDefault().Number ?? string.Empty
};
然后你的查询看起来像
var listOfCustomerSearchResult = entities.Customers.Where(x => x.Number.StartsWith(customerNumber)).Select(customerSelector).ToList();
答案 1 :(得分:0)
一种简单的方法是使用匿名类型收集这些额外信息。例如:
var listOfCustomerSearchResult = (from customer in entities.Customers
where customer.Number.StartsWith(customerNumber)
select new { CustomerSearchResult = new CustomerSearchResult
{
AccountNbr = (customer.Number.IndexOf(".") > 0) ? customer.Number.Substring(0, customer.Number.IndexOf(".")) : customer.Number,
SubAccountNbr = (customer.Number.IndexOf(".") > 0) ? customer.Number.Substring(customer.Number.IndexOf(".") + 1) : string.Empty,
FirstAndLastName = (customer.Contact.IsIndividual) ? (customer.Contact.FirstNameCareOf + " " ?? string.Empty) + (customer.Contact.Name ?? string.Empty) : (customer.Contact.Name ?? string.Empty) + " " + (customer.Contact.FirstNameCareOf ?? string.Empty),
StreetAddress = customer.Contact.Addresses.FirstOrDefault().StreetAddress ?? string.Empty,
City = customer.Contact.Addresses.FirstOrDefault().City ?? string.Empty,
ZipCode = customer.Contact.Addresses.FirstOrDefault().ZipCode ?? string.Empty,
Region = customer.Contact.Addresses.FirstOrDefault().Region.Code ?? string.Empty,
Delivery = string.Empty,
IsActive = customer.IsActive,
IsAdministrative = customer.IsAdministrative,
SearchStep = 1,
CustomerId = customer.Id,
AccountType = customer.Type.EnumId,
Phone = customer.Contact.Phones.FirstOrDefault().Number ?? string.Empty
}, FormatedPhone = FormatPhone(customer.Phone), ... ).Take(500).ToList();
这将使您无需搜索查询即可访问这些附加属性。