Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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# 按相关项目的数量顺序返回项目列表_C#_Linq_Entity Framework - Fatal编程技术网

C# 按相关项目的数量顺序返回项目列表

C# 按相关项目的数量顺序返回项目列表,c#,linq,entity-framework,C#,Linq,Entity Framework,我想返回一个项目列表,按照有多少相关项目的顺序排列 想象一下下面的课程。想象一下他们都有DBSET上下文.A…,上下文.B class A { public ID { get; set; } } class B { public virtual A A { get; set; } } 我正试图按与B最相关的顺序获取a项目的列表。查询可能如下所示: IEnumerable<A> GetMostRelatedAs( int numberOfAsToReturn ) {

我想返回一个项目列表,按照有多少相关项目的顺序排列

想象一下下面的课程。想象一下他们都有DBSET<代码>上下文.A…,
上下文.B

class A
{
    public ID { get; set; }
}

class B
{
    public virtual A A { get; set; }
}
我正试图按与
B
最相关的顺序获取
a
项目的列表。查询可能如下所示:

IEnumerable<A> GetMostRelatedAs( int numberOfAsToReturn )
{
    return this.context.A.SelectMany( 
        a => a.ID, 
        ( whatever) => new
        {
            A = whatever,
            RelatedBCount = this.context.B.Where( b => b.A.ID == whatever.ID)
        }).OrderByDescending( x => x.RelatedBCount ).Take( numberOfAsToReturn  );
}
IEnumerable GetMostRelatedAs(int numberOfAsToReturn)
{
返回此.context.A.SelectMany(
a=>a.ID,
(无论什么)=>新的
{
A=随便什么,
RelatedBCount=this.context.B.Where(B=>B.A.ID==whatever.ID)
}).OrderByDescending(x=>x.RelatedBCount).Take(numberOfAsToReturn);
}
我的查询哪里出错了?

原因如下:

我试图得到一个列表的项目,以最相关的顺序从B

到from
使这一点非常混乱,因此在此基础上,我将对这一点进行深入探讨:

IEnumerable<dynamic> GetMostRelatedAs( int numberOfAsToReturn )
{
    var results = this.context.A
        .GroupJoin(
            this.context.B,
            a => a.ID,
            b => b.A.ID,
            (singleA, multipleBs) => new {
                    // this is the projection, so take here what you want
                    numberOfBs = multipleBs.Count(),
                    name = singleA.Name,
                    singleA.ViewCount
                }
            )
        .OrderByDescending(x => x.ViewCount)
        .Take(numberOfAsToReturn)
        .ToList();

        // here you can use automapper to project to a type that you can use
        // So you could add the following method calls after the ToList()
        // .Project(this.mappingEngine)
        // .To<ClassThatRepresentsStructure>()

        // The reason you don't map before the ToList is that you are already doing a projection with that anonymous type.
    return results;
}
IEnumerable GetMostRelatedAs(int numberOfAsToReturn)
{
var results=this.context.A
.GroupJoin(
这是上下文,
a=>a.ID,
b=>b.A.ID,
(单A、多B)=>新{
//这是投影图,你要什么就拿什么
numberOfBs=multipleBs.Count(),
name=singleA.name,
singleA.ViewCount
}
)
.OrderByDescending(x=>x.ViewCount)
.乘坐(返回次数)
.ToList();
//在这里,您可以使用automapper将项目投影到您可以使用的类型
//因此,可以在ToList()之后添加以下方法调用
//.Project(此.mappingEngine)
//.至()
//不在ToList之前映射的原因是,您已经在使用该匿名类型进行投影。
返回结果;
}
编辑

针对这些评论:

IEnumerable<A> GetMostRelatedAs( int numberOfAsToReturn )
{
    var results = this.context.A
        .GroupJoin(
            this.context.B,
            a => a.ID,
            b => b.A.ID,
            (singleA, multipleBs) => new {
                    // this is the projection, so take here what you want
                    numberOfBs = multipleBs.Count(),
                    name = singleA.Name,
                    singleA.ViewCount,
                    singleA
                }
            )
        .OrderByDescending(x => x.ViewCount)
        .Take(numberOfAsToReturn)
        .ToList()
        .Select(x => x.singleA);

    return results;
}
IEnumerable GetMostRelatedAs(int numberOfAsToReturn)
{
var results=this.context.A
.GroupJoin(
这是上下文,
a=>a.ID,
b=>b.A.ID,
(单A、多B)=>新{
//这是投影图,你要什么就拿什么
numberOfBs=multipleBs.Count(),
name=singleA.name,
singleA.ViewCount,
辛格拉
}
)
.OrderByDescending(x=>x.ViewCount)
.乘坐(返回次数)
托利斯先生()
.选择(x=>x.singleA);
返回结果;
}

“我错在哪里?”-您可能错过了,因为不清楚您期望它做什么,它实际上做了什么,以及您试图解决这些差异。我编辑了我的OP。我希望这会有更多帮助。您不能在查询中使用
ABTuple
,因为SQL不理解它。第二,您可能需要调用
ToList()
,这样您就可以对数据库执行查询了。在classI上使用匿名类型我已经编辑了我的问题并删除了元组。有没有办法将动态转换回对象中?或者只选择查询中的singleA?如果不是的话,我最好使用我最初声明的元组类来让其他开发人员明白一切。我猜我不能只在返回的动态列表中执行foreach循环,因为我将无法声明新的a,因为它将不再具有数据库中的a ID。您可以在该
new{}
中投影出您想要的内容,所以你可以说
originalA=singleA
,但不可能返回IEnumerable,我总是要返回动态对象,然后在我的视图中使用它,对吗?如果你有SQL Profiler,看看由此生成的SQL,它可能看起来很脏,性能可能很差。因此,执行存储过程可能会更好