使用 lambda 表达式从对象集合中选择不同的对象
我们有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
这将仅为您提供不同的代码/描述对,但看起来这就是您感兴趣的全部。
或者,使用分组:
这将为您提供所有
BluePart< /code> 按 OEM 代码/描述分组的实体。
How about:
This will only get you the distinct code/description pairs, but it looks like that's all you're interested in.
Alternatively, using grouping:
This will get you all the
BluePart
entities grouped by OEM code/description.