Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

c# - 如何按外键分组但返回外物?(How can I group by a foreign key but return the foreign object?)

I've been working with EF for quite some time but I always come across these kinds of statements

(我已经与EF合作了一段时间,但我经常遇到这些声明)

.GroupBy(movies => movies.Category) //Category is a foreign object

And normally I wouldn't want this kind of line in my code because you're never quite sure by what the object compares to.

(通常,我不希望在代码中出现这种行,因为您永远不确定对象与之比较。)

But if I try to group by the foreign key like

(但是如果我尝试按外键分组)

.GroupBy(movies => movies.CategoryId) //CategoryId is the EF generated foreign key

I never get the object which I sometimes need in the result of the query.

(我从没得到查询结果中有时需要的对象。)

I've already looked at some other overrides of the GroupBy function but it seems to me that all of them return the key in some way.

(我已经看过GroupBy函数的其他一些重写,但是在我看来,它们所有人都以某种方式返回了键。)

One thing I tried was creating an anon object.

(我尝试的一件事是创建一个anon对象。)

.GroupBy(movies => new {movies.CategoryId, movies. Category})

But it threw an EF exception.

(但这引发了EF异常。)

So I've been using the first statement based on the assumption that EF translates it into a statement which groups by ID like this.

(因此,我一直基于EF将其转换为按ID分组的语句的假设来使用第一条语句。)

Does it really?

(真的吗)

Or is there some overload or trick I could use to make sure that I group by the Id?

(还是我可以使用一些过载或技巧来确保我按ID分组?)

  ask by Lucas Engleder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...