Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Linq to SQL查询(创建为IQueryable)中进行计数将获得所有行_C#_Asp.net Mvc_Linq_Linq To Sql - Fatal编程技术网

C# 在Linq to SQL查询(创建为IQueryable)中进行计数将获得所有行

C# 在Linq to SQL查询(创建为IQueryable)中进行计数将获得所有行,c#,asp.net-mvc,linq,linq-to-sql,C#,Asp.net Mvc,Linq,Linq To Sql,类Customer具有以下方法,该方法通过linq查询返回IQueryable以获取客户的活动销售额: public IQueryable<SalesHeader> QueryCurrentSales() { // this.SalesHeaders is an association defined in the DBML between // the Customer and SalesHeader tables (SalesHea

类Customer具有以下方法,该方法通过linq查询返回IQueryable以获取客户的活动销售额:

    public IQueryable<SalesHeader> QueryCurrentSales()
    {
        // this.SalesHeaders is an association defined in the DBML between
        // the Customer and SalesHeader tables (SalesHeader.CustomerId <-> Customer.Id).
        return this.SalesHeaders
            .Where(sh => sh.Status == 1).AsQueryable();
    }

我想问题在于我们使用了AsQueryable()方法,但我不知道实现我们预期目标的正确方法是什么。

您没有展示什么是
saleheaders
。您觉得需要执行
AsQueryable
这一事实表明,它返回的是
IEnumerable
,而不是
IQueryable
。。。这可能就是问题所在。如果您能显示
saleheaders
,我们可能会提供帮助

如果
SalesHeaders
属性返回的表达式实际上可以作为
IQueryable
返回,那么您可以不使用
AsQueryable
而直接强制转换为
IQueryable
,但在这种情况下,我建议改为更改属性类型

编辑:好的,如果它是一个实体集,那么直接修复它可能很困难

我知道这很难看,但是像这样的怎么样:

public IQueryable<SalesHeader> QueryCurrentSales()
{
    // You *may* be able to get away with this in the query; I'm not sure
    // what LINQ to SQL would do with it.
    int id = this.Id;

    return context.SalesHeaders
                  .Where(sh => sh.Status == 1 && sh.CustomerId == id);
}
public IQueryable QueryCurrentSales()
{
//在查询中,您*可能*能够*逃脱;我不确定
//LINQtoSQL将如何处理它。
int id=this.id;
返回context.salessheaders
其中(sh=>sh.Status==1&&sh.CustomerId==id);
}

在这里,我假设您有一些方法可以访问所讨论的数据上下文-我不记得在LINQ to SQL中是否有一种简单的方法可以做到这一点,但我希望如此。

您还没有展示什么是
SalesHeaders
。您觉得需要执行
AsQueryable
这一事实表明,它返回的是
IEnumerable
,而不是
IQueryable
。。。这可能就是问题所在。如果您能显示
saleheaders
,我们可能会提供帮助

如果
SalesHeaders
属性返回的表达式实际上可以作为
IQueryable
返回,那么您可以不使用
AsQueryable
而直接强制转换为
IQueryable
,但在这种情况下,我建议改为更改属性类型

编辑:好的,如果它是一个实体集,那么直接修复它可能很困难

我知道这很难看,但是像这样的怎么样:

public IQueryable<SalesHeader> QueryCurrentSales()
{
    // You *may* be able to get away with this in the query; I'm not sure
    // what LINQ to SQL would do with it.
    int id = this.Id;

    return context.SalesHeaders
                  .Where(sh => sh.Status == 1 && sh.CustomerId == id);
}
public IQueryable QueryCurrentSales()
{
//在查询中,您*可能*能够*逃脱;我不确定
//LINQtoSQL将如何处理它。
int id=this.id;
返回context.salessheaders
其中(sh=>sh.Status==1&&sh.CustomerId==id);
}

在这里,我假设您有一些方法可以访问所讨论的数据上下文-我不记得在LINQ to SQL中是否有一种简单的方法可以做到这一点,但我希望如此。

这是正确的。正在SQL Server上执行count()

// cust is an object of type Customer.
int rows = cust.QueryCurrentSales().AsQueryable<SalesHeaders>().count();
//cust是Customer类型的对象。
int rows=cust.QueryCurrentSales().AsQueryable().count();

这是正确的。正在SQL Server上执行count()

// cust is an object of type Customer.
int rows = cust.QueryCurrentSales().AsQueryable<SalesHeaders>().count();
//cust是Customer类型的对象。
int rows=cust.QueryCurrentSales().AsQueryable().count();

@marc\s:我想你误解了这个问题。我相信它会将所有实际数据拉回到客户端并在那里进行计数。为什么需要
AsQueryable()
Where
方法是否默认返回一个
IQueryable
。这取决于调用Where方法的内容。@Jon Sekeet:是的,这就是问题所在@我需要它,而且这不是我唯一有问题的地方。假设我想对结果进行分页,然后执行“cust.QueryCurrentSales().take(10.toList()”。目前,Linq只有在已经检索到查询中的所有SalesHeader行(可能很多)之后才执行.take(10)方法。@marc_s:我想你误解了这个问题。我相信它会将所有实际数据拉回到客户端并在那里进行计数。为什么需要
AsQueryable()
Where
方法是否默认返回一个
IQueryable
。这取决于调用Where方法的内容。@Jon Sekeet:是的,这就是问题所在@我需要它,而且这不是我唯一有问题的地方。假设我想对结果进行分页,然后执行“cust.QueryCurrentSales().take(10.toList()”。目前,Linq只有在已经检索到查询中的所有SalesHeader行(可能很多)之后才执行.take(10)方法。我在原始文章中为代码添加了一条注释。SalesHeader是我在DBML中定义Customer和SalesHeader表之间的关联时自动创建的属性。不幸的是,做演员没有用。我已经检查过直接从上下文“context.SalesHeaders.Where(…)”获取数据,但它看起来像是。Where总是返回IEnumerable:SDisregard上一条注释的最后一行。似乎是这样。Where(当从上下文而不是通过关联访问数据时)可以同时分配给IQueryable和IEnumerable变量。而且它正确地创建了最后一个SQL查询(是的!),这是一个遗憾,因为这个.SalesHeaders.Where(…)是一个更好的解决方法。你知道如果我们使用Linq to Entities来访问SQL Server,它是否会起作用吗?@salgiza:我不知道Linq to Entities是否会更好。我在原始帖子中对代码添加了一条注释。SalesHeader是我在DBML中定义Customer和SalesHeader表之间的关联时自动创建的属性。不幸的是,做演员没有用