初学者 Linq 语法和 EF4 问题

发布于 2024-10-10 15:13:33 字数 650 浏览 0 评论 0原文

问题

通过以下 linq 代码片段,我得到了一个客户端列表,其地址已按规范过滤,但返回的实体的形式不是我所期望的。

数据为 1 个客户端有 2 个地址和 1 个客户端有 1 个地址。

该查询返回 3 行客户端,每行有 1 个地址

  • Client 1 =>地址 1
  • 客户端 1 =>地址2
  • 客户端2 =>地址3

    var query = 来自 context.Clients.Where(specification.SatisfiedBy()).Include("ClientAddresses") 中的 t1
                在 context.ClientAddresses.Where(spec.SatisfiedBy()) 中加入 t2
                t1.ClientKey 等于 t2.ClientKey
                选择t1;
    

我的期望更像是一个只有两个客户端的列表,一个客户端包含两个地址的集合,一个客户端包含一个地址的集合。

  • 客户端1 =>地址 1 / 地址 2
  • 客户端 2 =>地址3

我缺少什么???

谢谢!

Question

With the following linq code snip I get a list of clients with address filtered by the specifications but the form of the entities returned is not what i had expected.

The data is 1 client with 2 addresses and 1 client with 1 address.

The query returns 3 rows of clients each with 1 address

  • Client 1 => Address1
  • Client 1 => Address2
  • Client 2 => Address3

    var query = from t1 in context.Clients.Where(specification.SatisfiedBy()).Include("ClientAddresses")
                join t2 in context.ClientAddresses.Where(spec.SatisfiedBy())
                on t1.ClientKey equals t2.ClientKey
                select t1;
    

My expectation was a little more like a list with only two clients in it, one client with a collection of two addresses and one client with a collection of one address.

  • Client 1 => Address1 / Address2
  • Client 2 => Address3

What am I missing???

Thanks!

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

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

发布评论

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

评论(1

旧街凉风 2024-10-17 15:13:33

你尝试过类似的事情吗:
query = query.Distinct(); ?

您可能需要揭示规范的编写方式以提供更多数据。

例如,我不明白为什么您的查询不是这样的:

var query = from t1 in context.Clients.Include("ClientAddresses")
            where specification.SatisfiedBy() &&
                t1.ClientAddresses.Any(spec.SatisfiedBy())
            select t1;

更新

看看这是否有效。不确定 EF 支持其中多少。它与您原来的查询非常相似

var query = (from t1 in context.Clients.Where(specification.SatisfiedBy())
                 .Include("ClientAddresses")
             from t2 in context.ClientAddresses.Where(spec.SatisfiedBy())
             where t1.ClientKey == t2.ClientKey
             select t1)
            .Distinct();

did you try something like:
query = query.Distinct(); ?

You might need to reveal how the specifications are written to give more data.

For example, I don't see why your query is not something like:

var query = from t1 in context.Clients.Include("ClientAddresses")
            where specification.SatisfiedBy() &&
                t1.ClientAddresses.Any(spec.SatisfiedBy())
            select t1;

Update

See if this works. Not sure how much of that is supported by EF. It's very similar to your oriignal query

var query = (from t1 in context.Clients.Where(specification.SatisfiedBy())
                 .Include("ClientAddresses")
             from t2 in context.ClientAddresses.Where(spec.SatisfiedBy())
             where t1.ClientKey == t2.ClientKey
             select t1)
            .Distinct();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文