标签: iqueryable

NHibernate测试,嘲笑ISession

我正在使用NHibernate和Rhinomocks,并且无法测试我想要的内容.我想在不命中数据库的情况下测试以下存储库方法(其中_session作为ISession注入到存储库中):

public class Repository : IRepository
{
    (... code snipped for brevity ...)

    public T FindBy<T>(Expression<Func<T, bool>> where)
    {  
        return _session.Linq<T>().Where(where).FirstOrDefault();
    }
}
Run Code Online (Sandbox Code Playgroud)

我最初的方法是模拟ISession,并在调用Linq时返回IQueryable存根(手动编码).我有一个客户对象的IList,我想在memeory中查询以测试我的Linq查询代码而不会访问db.我不确定这会是什么样子.我是否编写自己的IQueryable实现?如果是这样,有人为这种方法做了这个吗?或者我需要查看其他途径?

谢谢!

nhibernate iqueryable mocking

6
推荐指数
1
解决办法
3975
查看次数

如何在Entity Framework 4中的POCO中定义集合?

假设我有一个包含0个或更多玩家Team类.

播放器类很简单:

public class Player
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Team Team { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

但最好定义Team类?

选项1

public class Team
{
    public long Id { get; set; }
    public string Name { get; set; }
    public ICollection<Player> Players { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

选项2:

public class Team
{
    public Team()
    {
        Players = new Collection<Player>();
    }

    public long …
Run Code Online (Sandbox Code Playgroud)

collections entity-framework iqueryable poco

6
推荐指数
1
解决办法
1539
查看次数

创建空IQueryable <T>对象时出现问题

基本上我想将两个Iqueryable合并为一个Iqueryable,然后在循环结束后返回完整的记录集.它运行完美,但最后我的objret什么都没有,但当我调试循环obj有一些记录.我做错了

IQueryable<MediaType> objret = Enumerable.Empty<MediaType>().AsQueryable();
var typ = _db.MediaTypes.Where(e => e.int_MediaTypeId != 1 && e.int_MediaTypeId_FK == null).ToList();
for (int i = 0; i < typ.Count; i++)
{ 
    IQueryable<MediaType> obj = _db.MediaTypes.Where(e => e.bit_IsActive == true && e.int_MediaTypeId_FK == typ[i].int_MediaTypeId);
    IQueryable<MediaType> obj1 = _db.MediaTypes.Where(e => e.int_OrganizationId == Authorization.OrganizationID && e.bit_IsActive == true && e.int_MediaTypeId_FK == typ[i].int_MediaTypeId);

    if (obj1.Count() > 0)
        obj.Concat(obj1);
    if(obj.Count() > 0)
        objret.Concat(obj);
}
return objret;
Run Code Online (Sandbox Code Playgroud)

c# collections iqueryable

6
推荐指数
1
解决办法
6147
查看次数

实体框架转换错误

以下工作完美:

IQueryable<Property> PropertyQuery = PropertyDAO.SearchWithAdditionalParameters(/* elided */);
IQueryable<long> propertyIdQuery = PropertyQuery.Select(p => p.PropertyId);

var relevantFMVs = PropertyDAO.db.FMVHistories.Where(f => propertyIdQuery.Contains(f.PropertyId)).ToList();
Run Code Online (Sandbox Code Playgroud)

但是以下情况爆发了:

IQueryable<Property> PropertyQuery = PropertyDAO.SearchWithAdditionalParameters(/* elided */);

var relevantFMVs = PropertyDAO.db.FMVHistories.Where(f => PropertyQuery.Select(p => p.PropertyId).Contains(f.PropertyId)).ToList();
Run Code Online (Sandbox Code Playgroud)

(请注意,我没有单独创建propertyIdQuery,而只是将变量本身替换为查询本身)

例外是

无法转换类型'System.Linq.IQueryable 1' to type 'System.Linq.IQueryable1'.LINQ to Entities仅支持转换实体数据模型基元类型.

有人可以了解EF(4)在封面下做什么只做第一次查询工作,即使它们表面上是等效的吗?

我知道IQueryable<T>,表达树在幕后做了很多事情,但是如何将中间步骤保存到局部变量会影响结果呢?

编辑

根据请求,这是被调用的完整方法,以及该方法调用的方法:

    public IQueryable<Property> BasicSearchFromConstraints(PropertyInvoiceConstraints constraints) {
        return ExecuteSearchFromConstraints((dynamic)constraints.PropertyInst, constraints.CompanyNumber, constraints.TaxSubType, constraints.PhaseID, constraints.State, constraints.County, constraints.City, constraints.Jurisdiction);
    }

    private IQueryable<T> ExecuteSearchFromConstraints<T>(T property, int CompanyNumber, byte SubType, byte PhaseID, string State, string County, string …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework iqueryable entity-framework-4

6
推荐指数
1
解决办法
1816
查看次数

返回Enumerable.Empty <T>().AsQueryable()一个坏主意?

最好用一些代码解释这个:

public IQueryable<DatabaseRecord> GetQueryableLinkedRecords()
{
    if(this.currentlyHeldIds.Count() == 0)
    {
        return Enumerable.Empty<DatabaseRecord>().AsQueryable();
    }
    else
    {
        return from r in this.DBContext.DatabaseRecords
               where this.currentlyHeldIds.Contains(r.Id)
               select r;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个想法是,如果没有查询当前的HelloId,则没有理由再次实际查询数据库.如果currentHeldIds没有值,LINQ to SQL仍将查询db.这种方法有什么问题吗?我意识到还有一些其他问题与返回IQueryable一般有关,但抛开那些论点,试图绕过这样的db调用有什么不对吗?

.net c# linq iqueryable linq-to-sql

6
推荐指数
1
解决办法
1022
查看次数

IQueryable序列化(web api)

为什么这不起作用?:

        var surveys = db.Surveys.Where(s => s.Author.UserId == user.UserId);

        return from survey in surveys
               select new
               {
                   surveyId = survey.SurveyId,
                   title = survey.Title
               };
Run Code Online (Sandbox Code Playgroud)

而这一点,有一个小小的改变,是?:

        var surveys = db.Surveys.Where(s => s.Author == user);

        return from survey in surveys
               select new
               {
                   surveyId = survey.SurveyId,
                   title = survey.Title
               };
Run Code Online (Sandbox Code Playgroud)

它会引发序列化错误

The 'ObjectContent`1' type failed to serialize the response body for content type 
'application/xml; charset=utf-8'.  (...)
Run Code Online (Sandbox Code Playgroud)




我很好解决这个问题,但我在这里(下面)有同样的错误,并且不能以同样的方式解决它:

var surveys = db.Surveys.Where(s => s.AnswerableBy(user));
Run Code Online (Sandbox Code Playgroud)

serialization iqueryable asp.net-web-api

6
推荐指数
1
解决办法
4389
查看次数

IQueryable vs IQueryable &lt;T&gt;

我只是想知道为什么会有IQueryable<T>没有通用功能的版本?

c# iqueryable

6
推荐指数
1
解决办法
1589
查看次数

实体框架IQueryable与poco生成

我已经创建了一个T4模板,它为每个属性生成标准的Entities类和Interfaces,这样我就可以制作只包含我想要的数据的自定义poco对象.我还创建了一个复制函数,它可以在实现所述实体接口的任何对象之间进行转换

生成的代码如下所示

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace DomainModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;


    public interface IRole
    {
    }
    public interface IRole_RoleId : IRole
    {
      int RoleId { get; set; }
    }
    public interface IRole_ApplicationName : IRole
    {
      string ApplicationName { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework lazy-loading iqueryable poco

6
推荐指数
1
解决办法
1402
查看次数

使用Fluent.NHibernate,如何基于Select语句执行Insert

我有一个相当复杂的逻辑,它IQueryable为我生成一个用于从我的数据库返回数据的方法Fluent.NHibernate.

但是,我需要能够将此查询的结果存储回数据库(实际上只是主键,但这是一个副作用)

如何生成基于IQueryable我已经获得SQL 的插入语句,如下面生成的示例所示:

INSERT INTO MySavedResults (Id, FirstName, LastName)
SELECT Id, FirstName, LastName FROM Member
WHERE 
FirstName = 'John' and LastName ='Snow' and ...-- more conditions
Run Code Online (Sandbox Code Playgroud)

c# sql-server iqueryable fluent-nhibernate

6
推荐指数
1
解决办法
822
查看次数

源IQueryable的提供程序未实现IAsyncQueryProvider

我有一些类似下面的代码,我想编写方法的单元测试。但是我陷入了异步方法。你能帮我吗 ?

public class Panel
{
    public int Id { get; set; }
    [Required] public double Latitude { get; set; }
    public double Longitude { get; set; }
    [Required] public string Serial { get; set; }
    public string Brand { get; set; }
}

public class CrossSolarDbContext : DbContext
{
    public CrossSolarDbContext()
    {
    }

    public CrossSolarDbContext(DbContextOptions<CrossSolarDbContext> options) : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

public interface IGenericRepository<T>
{ …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing iqueryable entity-framework-6 asp.net-core-2.0

6
推荐指数
4
解决办法
7053
查看次数