ExecuteQuery 是否可以返回 DBML 生成的类而不获取该类的所有信息?

发布于 2024-09-07 20:19:00 字数 518 浏览 6 评论 0原文

我有几个 DBML 生成的类,它们通过 id 链接在一起,例如

ClassA {
    AID,
    XID,
    Name
}

ClassB {
    AID,
    ExtraInfo,
    ExtraInfo2
}

在使用 db.ClassAs.Where(XID == x) 之类的东西并迭代该结果时, 它最终会对每个 ClassA 和每个 ClassB 执行一个查询,这很慢。

或者,我尝试使用 ExecuteQuery 来获取我关心的所有信息并返回 ClassA。在迭代过程中,我最终会做同样的事情,即进行大量单独的提取,而不是仅进行 1 次。如果我将其存储在 ClassC 中(不与数据库实体关联),该 C 类具有两个 ClassA 感兴趣的字段和 ClassB,这个查询要快得多,但是很烦人,因为我刚刚创建了一个不必要的 ClassC。

我怎样才能仍然使用与 ClassB 关联的 ClassA,并且仍然使用 ExecuteQuery 来运行 1 个查询(相对于 A*B 数量的查询)?

I have a couple of DBML generated classes which are linked together by an id, e.g.

ClassA {
    AID,
    XID,
    Name
}

ClassB {
    AID,
    ExtraInfo,
    ExtraInfo2
}

In using something like db.ClassAs.Where(XID == x) and iterating through that result,
it ends up executing a query for each of the ClassAs and each of ClassBs, which is slow.

Alternatively, I've tried to use ExecuteQuery to fetch all the info I care about and have that return a ClassA. In iterating over that I end up with it doing the same, i.e. doing alot of individual fetches vs. just 1. If I store it in a ClassC (that is not associated with a DB entity) which has the fields of interest of both ClassA and ClassB, this query is much faster, but it's annoying b/c I just created IMO an unnecessary ClassC.

How can I still use ClassA, which associates to ClassB, and still use ExecuteQuery to run 1 query vs. A*B number of queries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-09-14 20:19:00

如果您有关联,则您不应该需要使用ExecuteQuery()。

这是一个使用一些虚构的图书库上下文和匿名类型作为结果的示例:

var results = 
    Books
    .Where(book => book.BookId == 1)
    .Select(book =>
        new
        {
            book.Name,
            AuthorName = book.Author.Name,  //Is a field in an associated table.
            book.Publisher, //Is an associtated table.
        });

编辑:没有匿名类型

var results = 
        Books
        .Where(book => book.BookId == 1)
        .Select(book =>
            new BookResult()
            {
                BookName = book.Name,
                AuthorName = book.Author.Name,  //Is a field in an associated table.
                Publisher = book.Publisher, //Is an associtated table.
            });

If you have associations you shouldn't need to use ExecuteQuery().

Here's an example using some imaginary Book Library context and anonymous types for the result:

var results = 
    Books
    .Where(book => book.BookId == 1)
    .Select(book =>
        new
        {
            book.Name,
            AuthorName = book.Author.Name,  //Is a field in an associated table.
            book.Publisher, //Is an associtated table.
        });

EDIT: without anon types

var results = 
        Books
        .Where(book => book.BookId == 1)
        .Select(book =>
            new BookResult()
            {
                BookName = book.Name,
                AuthorName = book.Author.Name,  //Is a field in an associated table.
                Publisher = book.Publisher, //Is an associtated table.
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文