链接到实体-格式化结果
本文关键字:格式化 结果 实体 链接 | 更新日期: 2023-09-27 18:15:28
我在格式化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-我需要格式化地址,电话等,但我不能直接在选择新…是否有一种方法可以调用我的"助手函数"(如FormatPhoneNumber(), FormatAddress(),…),而无需扫描所有结果并在执行查询后逐个格式化每个结果?例如:像MyQuery…blabla…Take(500). tolist()。ImaginaryFormatProperties(x=> x.Phone = FormatPhone(x.Phone), x.Address = FormatAddress(x.Address)…
2-我需要多次调用这个查询,每次都有大量不同的WHERE子句。有没有一种方法可以做到这一点,而不必每次都创建CustomerSearchResult并分配这样的每个属性??我不想每次都重复这个设置部分,因为它没有改变
谢谢!
标题>标题>1。您可以在CustomerSearchResult模型类中添加一个只读属性来为您执行格式化。
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)。你想创建一个可查询的结果,如下所示:
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();
一种简单的方法是使用匿名类型收集这些额外的信息。例如:
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();
这将使您无需在查询中执行搜索即可访问这些附加属性。