【问题标题】:How to cast an Collection<x> to an IQueryable<x>如何将 Collection<x> 转换为 IQueryable<x>
【发布时间】:2011-08-19 10:38:20
【问题描述】:

我正在尝试设置最小起订量,但我需要创建一个假的 IQueryable。我做了一个集合,但我不知道如何将它转换为 IQueryable。

Collection<DetailDataEntity> DetailDataEntityCollection = 
    new Collection<DetailDataEntity>();

DetailDataEntity DetailDataEntity = new DetailDataEntity();
DetailDataEntity.FeedTypeID = 1;
DetailDataEntityCollection.Add(DetailDataEntity);


_mockRepository.Setup(x => x.GetDetail(It.IsAny<Int32>(),
                                       It.IsAny<Enum.FeedTypeEnum.FeedType>())) 
               .Returns(DetailDataEntityCollection);

【问题讨论】:

    标签: c# linq collections


    【解决方案1】:

    只需在您的收藏中致电AsQueryable

    _mockRepository.Setup(x => x.GetDetail(It.IsAny<Int32>(), 
                                           It.IsAny<Enum.FeedTypeEnum.FeedType>()))
                   .Returns(DetailDataEntityCollection.AsQueryable());
    

    【讨论】:

      【解决方案2】:

      我有一个简单的 ICollection 对象,发现它对我有用:

      var tapSettings = xfmrTapSettings.ToList().AsQueryable();
      

      我必须在 AsQueryable 前面使用 ToList 才能使其工作。

      【讨论】:

        猜你喜欢
        • 2021-12-24
        • 2016-09-13
        • 2012-03-06
        • 1970-01-01
        • 2010-12-26
        • 1970-01-01
        • 2011-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多