【发布时间】:2017-06-25 15:02:22
【问题描述】:
这是我的代码:
private void loadList(IQueryable<customer> customers)
{
ListView.Items.Clear();
foreach (var customer in customers)
{
ListViewItem item = new ListViewItem(customer.customerNumber.ToString());
item.SubItems.Add(customer.customerName);
item.SubItems.Add(customer.contactFirstName);
item.SubItems.Add(customer.contactLastName);
item.SubItems.Add(customer.phone);
item.SubItems.Add(customer.addressLine1);
item.SubItems.Add(customer.addressLine2);
item.SubItems.Add(customer.city);
item.SubItems.Add(customer.state);
item.SubItems.Add(customer.postalCode);
item.SubItems.Add(customer.country);
item.SubItems.Add(customer.salesRepEmployeeNumber.ToString());
item.SubItems.Add(customer.creditLimit.ToString());
ListView.Items.AddRange(new ListViewItem[] { item });
}
}
private static readonly Func<customer, string>[] _searches;
static Main()
{
_searches = new Func<customer, string>[]
{
(c) => c.customerNumber.ToString(),
(c) => c.customerName,
(c) => c.contactFirstName,
(c) => c.contactLastName,
(c) => c.phone,
(c) => c.addressLine1,
(c) => c.addressLine2,
(c) => c.city,
(c) => c.state,
(c) => c.postalCode,
(c) => c.country,
(c) => c.salesRepEmployeeNumber.ToString(),
(c) => c.creditLimit.ToString(),
};
}
protected virtual void SearchBox_TextChanged(object sender, EventArgs e)
{
var search = _searches[SearchItem.SelectedIndex];
CustomerContext context = new CustomerContext();
IQueryable<customer> customers = from x in context.customers
where search(x).Contains(SearchBox.Text)
select x;
loadList(customers);
}
我在 foreach 开始时的 loadList 方法中收到此错误:
'LINQ to Entities 不支持 LINQ 表达式节点类型'Invoke'。'
我该如何解决这个问题?我对这一切都很陌生,我尝试了几件事,但都没有奏效。
【问题讨论】:
-
将
_searches = new Func<customer, string>[]更改为_searches = new Expression<Func<customer, string>>[]然后使用 LinqKit:stackoverflow.com/questions/36736203/combining-expression-trees -
好的,但在问题中 Linq 是在链中使用的(或任何你称之为的)。我怎样才能像我一样使用 Linq 来做到这一点?
标签: c# entity-framework linq