1

我有两个名为 OfflineOrderLineItem.cs 和 OnlineOrderLineItem.cs 的类都有名为离线和在线的不同订单表

在那我想结合这两个表数据来搜索和显示两个表中的字段

如何在 mvc4 中使用 linq 来做到这一点???任何想法.....

public virtual IPagedList<OnlineOrderLineItem> SearchOrderLineItems(string PoNumber)
{
     var query1 = (from ol in _offlineOrderLineItemRepository.Table
                   select new
                  {                            
                   ol.Name

                  }).ToList();

     var query2 = (from opv in _onlineOrderLineItemRepository.Table
                   select new
                   {
                    opv.Name

                   }).ToList();

     var finalquery = query1.Union(query2);

     if (!String.IsNullOrWhiteSpace(Name))
     finalquery = finalquery.Where(c => c.Name == Name);
     var orderlineitems = finalquery.ToList(); //its not working  it throw a error
     return new PagedList<OnlineOrderLineItem>(orderlineitems);//error 
    }

错误

cannot convert from 'System.Collections.Generic.List<AnonymousType#1>'
to 'System.Linq.IQueryable<Nop.Core.Domain.Management.OnlineOrderLineItem>'
to 'System.Linq.IQueryable<Nop.Core.Domain.Management.OnlineOrderLineItem>'
4

2 回答 2

2

query1并且query2是具有 type 单个属性的匿名类型列表string。(我假设ol.Nameandopv.Namestrings。)因此finalQueryandorderlineitems也是这个匿名的集合。通过指定PagedList<T>,您要求传递给构造函数的集合是类型的枚举TTOnlineOrderLineItem,但传递给构造函数的枚举是匿名类型,它是另一种类型。结果:编译器错误。

为了解决这个问题,我建议您定义一个命名的帮助器类型,您可以使用它来合并两种不同的类型OfflineOrderLineItemOnlineOrderLineItem

public class OrderLineItemViewModel
{
    public int Id { get; set; }
    public string PoNumber { get; set; }
    public string Name { get; set; }

    // maybe more common properties of `OfflineOrderLineItem`
    // and `OnlineOrderLineItem`     
}

然后您的SearchOrderLineItems方法应该返回该助手类型的分页列表:

public virtual IPagedList<OrderLineItemViewModel> SearchOrderLineItems(
    string PoNumber)
{
    var query1 = from ol in _offlineOrderLineItemRepository.Table
                 select new OrderLineItemViewModel
                 {
                     Id = ol.Id,
                     PoNumber = ol.PoNumber,
                     Name = ol.Name,
                     // maybe more properties
                 };
                 // don't use ToList here, so that the later Union and filter
                 // can be executed in the database

    var query2 = from opv in _onlineOrderLineItemRepository.Table
                 select new OrderLineItemViewModel
                 {
                     Id = opv.Id,
                     PoNumber = opv.PoNumber,
                     Name = opv.Name,
                     // maybe more properties
                 };
                 // don't use ToList here, so that the later Union and filter
                 // can be executed in the database

    var finalquery = query1.Union(query2);
    // again no ToList here

    if (!string.IsNullOrWhiteSpace(PoNumber))
        finalquery = finalquery.Where(c => c.PoNumber == PoNumber);

    var orderlineitems = finalquery.ToList(); // DB query runs here

    return new PagedList<OrderLineItemViewModel>(orderlineitems);
}

重要的是ToList只在查询的最后使用。OnlineOrderLineItem否则,您会将所有s 和所有s的整个表加载OfflineOrderLineItem到内存中,然后过滤掉PoNumber内存中给定的项目,这将是一个很大的开销和性能灾难。

于 2013-03-09T19:22:05.813 回答
1

代替

 var orderlineitems = finalquery.ToList();

尝试

 var orderlineitems = finalquery.AsQueryable();

https://github.com/TroyGoode/PagedList/blob/master/src/PagedList/PagedList.cs, PagedList 需要一个IQueryable<T>

Queryable.AsQueryable<TElement> Method

于 2013-02-05T17:11:45.747 回答