Linq to entities - 格式化结果
Linq to entities - Formatting results
我在格式化 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- 我需要格式化地址,phone 等等,但我不能直接在 Select new 中完成...有没有办法调用我的 "helper fonction" (例如 FormatPhoneNumber(), FormatAddress(), ...) 而不是扫描所有结果并在执行查询后逐一格式化每个结果?例如:像 MyQuery...blabla...Take(500).ToList().ImaginaryFormatProperties(x=> x.Phone = FormatPhone(x.Phone), x.Address = FormatAddress(x.Address) ...
2- 我需要多次调用此查询,每次使用大量不同的 WHERE 子句。有没有办法做到这一点,而不必每次都创建 CustomerSearchResult 并像这样分配每个属性???我不想每次都重复这个设置部分,因为它没有改变
谢谢!
一种简单的方法是使用匿名类型收集这些额外信息。例如:
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();
这将使您无需通过查询执行搜索即可访问这些附加属性。
1.)
您可以将只读 属性 添加到您的 CustomerSearchResult 模型 class 来为您进行格式化。
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 进行查询。 Sql 在您调用 ToList()
之前不会执行
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();
我在格式化 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- 我需要格式化地址,phone 等等,但我不能直接在 Select new 中完成...有没有办法调用我的 "helper fonction" (例如 FormatPhoneNumber(), FormatAddress(), ...) 而不是扫描所有结果并在执行查询后逐一格式化每个结果?例如:像 MyQuery...blabla...Take(500).ToList().ImaginaryFormatProperties(x=> x.Phone = FormatPhone(x.Phone), x.Address = FormatAddress(x.Address) ...
2- 我需要多次调用此查询,每次使用大量不同的 WHERE 子句。有没有办法做到这一点,而不必每次都创建 CustomerSearchResult 并像这样分配每个属性???我不想每次都重复这个设置部分,因为它没有改变
谢谢!
一种简单的方法是使用匿名类型收集这些额外信息。例如:
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();
这将使您无需通过查询执行搜索即可访问这些附加属性。
1.) 您可以将只读 属性 添加到您的 CustomerSearchResult 模型 class 来为您进行格式化。
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 进行查询。 Sql 在您调用 ToList()
之前不会执行 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();