使用 lambda 表达式从对象集合中选择不同的对象

发布于 2024-09-11 18:33:57 字数 998 浏览 2 评论 0原文

我们有一个使用 Fluent NHibernate 的项目。有一个名为 BluePart 的对象,其属性为 Oem 类型的 Oem。

public class BluePart : DomainEntity
{
    ...
    public virtual Oem Oem { get; set; }
}

Oem 对象具有多个属性,包括 OemCode 和 OemDescription。

public class Oem : DomainEntity
{
    ...
    public virtual string OemCode { get; set; }
    public virtual string OemDescription { get; set; }
}

我正在尝试使用 lambda 表达式构建一个 linq 查询,该表达式将从 BlueParts 列表(270 万条记录)中获取所有不同的 Oem。理想情况下,它应该生成以下 sql(运行时间小于 1 秒):

select distinct o.OemCode, o.OemDescription 
From BluePart b inner join Oem o on o.OemId = b.Oem_id

下面是我构建的查询,它返回所有 Oem,无论其独特性如何。

var oem = repository.Query<BluePart>().Select(x => new Oem { OemCode =
x.Oem.OemCode, OemDescription = x.Oem.OemDescription}).ToList();

我认为这个查询很容易构建,但事实并非如此。运行 GroupBy (.GroupBy(z => z.OemCode)) 时,我不断收到错误消息,指出我尝试 GroupBy 的属性不是 Bluepart 的属性(它不应该是这样,因为我正在对OEM 的财产)

We have a project using Fluent NHibernate. There is an object called BluePart with a property of Oem of type Oem.

public class BluePart : DomainEntity
{
    ...
    public virtual Oem Oem { get; set; }
}

The Oem object has several properties including OemCode and OemDescription.

public class Oem : DomainEntity
{
    ...
    public virtual string OemCode { get; set; }
    public virtual string OemDescription { get; set; }
}

I am trying to build a linq query using lambda expressions that will get all distinct Oems from a list of BlueParts (2.7million records). Ideally it should produce the following sql (which runs in <1sec):

select distinct o.OemCode, o.OemDescription 
From BluePart b inner join Oem o on o.OemId = b.Oem_id

Below is the query I built which returns all Oems, regardless of distinctness.

var oem = repository.Query<BluePart>().Select(x => new Oem { OemCode =
x.Oem.OemCode, OemDescription = x.Oem.OemDescription}).ToList();

I thought this query would be easy to build but it's not turning out to be that way. When running a GroupBy (.GroupBy(z => z.OemCode)), I keep getting an error saying the property I try to GroupBy is not a property of Bluepart (which it shouldn't be because I'm grouping on a property of Oem)

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

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

发布评论

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

评论(1

杀お生予夺 2024-09-18 18:33:57

怎么样:

var oem = repository.Query<BluePart>()
                    .Select(x => new { OemCode = x.Oem.OemCode, 
                                       OemDescription = x.Oem.OemDescription})
                    .Distinct()
                    .ToList();

这将为您提供不同的代码/描述对,但看起来这就是您感兴趣的全部。

或者,使用分组:

var oem = repository.Query<BluePart>()
                    .GroupBy(x => new { OemCode = x.Oem.OemCode, 
                                        OemDescription = x.Oem.OemDescription})
                    .ToList();

这将为您提供所有BluePart< /code> 按 OEM 代码/描述分组的实体。

How about:

var oem = repository.Query<BluePart>()
                    .Select(x => new { OemCode = x.Oem.OemCode, 
                                       OemDescription = x.Oem.OemDescription})
                    .Distinct()
                    .ToList();

This will only get you the distinct code/description pairs, but it looks like that's all you're interested in.

Alternatively, using grouping:

var oem = repository.Query<BluePart>()
                    .GroupBy(x => new { OemCode = x.Oem.OemCode, 
                                        OemDescription = x.Oem.OemDescription})
                    .ToList();

This will get you all the BluePart entities grouped by OEM code/description.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文