LINQ&多对多关系

发布于 2024-11-04 05:11:24 字数 895 浏览 0 评论 0原文

所以我正在编写一个“动态”Linq 查询。我创建了一个“选项”类,其中包含可以作为查询一部分的所有动态选项。其中一些选项属性是 List 对象,它们保存我想要返回的实体的 ID,这些实体是 SQL Server 中多对多关系的一部分。快速代码示例和表格描述可能会有所帮助(为了简洁起见,认真精简)。

桌车: ID int PK, 型号 varchar(50), 年份 int

表格颜色: ID int PK, 名称 varchar(50)

Table CarsXColors: CarId int PK, ColorId int PK

public IEnumerable<Car> Search(SearchOptions options)
{
    var query = from car in ctx.Cars select car;

    // This works just fine
    if (options.MaxMileage.HasValue) query = query.Where(x => x.Mileage <= options.Mileage.Value);

    // How do I implement this pseudo code. options.Colors is a List<int>
    if (options.Colors.Count > 0)
    {
        query = query.Where(  -- select cars that are in the List<int> of colors --);
    }


    return query;
}

So I'm writing a "dynamic" Linq query. I've created an "options" class that holds all of the dynamic options that can be a part of the query. Some of these option properties are List objects, which hold IDs of entities that I want to return that are part of many-to-many relationships in SQL Server. A quick code example and descriptions of the tables might help (seriously pared down for brevity).

Table Cars:
Id int PK,
Model varchar(50),
Year int

Table Colors:
Id int PK,
Name varchar(50)

Table CarsXColors:
CarId int PK,
ColorId int PK

public IEnumerable<Car> Search(SearchOptions options)
{
    var query = from car in ctx.Cars select car;

    // This works just fine
    if (options.MaxMileage.HasValue) query = query.Where(x => x.Mileage <= options.Mileage.Value);

    // How do I implement this pseudo code. options.Colors is a List<int>
    if (options.Colors.Count > 0)
    {
        query = query.Where(  -- select cars that are in the List<int> of colors --);
    }


    return query;
}

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

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

发布评论

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

评论(3

去了角落 2024-11-11 05:11:24

我看到您已经得到了答案,但也要考虑这种替代方案:

var query = 
    from car in ctx.Cars 
    where options.Colors.Count <= 0 || car.CarsXColors.Any(y => options.Colors.Contains(y.Id))
    select car;

这种风格很常见,因为它始终生成相同的 SQL 语句,以便数据库引擎或 LINQ 实际上可以缓存查询和/或查询计划以实现更快的响应。

如果 options.Colors 为空,数据库查询优化器将自动消除 WHERE 子句,因此您无需为此付出任何性能损失。

I see that you already got the answer, but also consider this alternative:

var query = 
    from car in ctx.Cars 
    where options.Colors.Count <= 0 || car.CarsXColors.Any(y => options.Colors.Contains(y.Id))
    select car;

This style is quite common as it generates the same SQL statement throughout so that database engines or LINQ can actually cache the query and/or query plan for faster response.

The database query optimizer will automatically eliminate the WHERE clause if options.Colors is empty, so you are not paying any performance penalties here.

扭转时空 2024-11-11 05:11:24
   query = query.Where(x => options.Colors.Contains(x.ColorID))
   query = query.Where(x => options.Colors.Contains(x.ColorID))
残花月 2024-11-11 05:11:24

回答了我自己的问题...

if (options.Colors.Count > 0) 
{
    query = query.Where(x => x.CarsXColors.Any(y => options.Colors.Contains(x.Id)));
}

Answered my own question...

if (options.Colors.Count > 0) 
{
    query = query.Where(x => x.CarsXColors.Any(y => options.Colors.Contains(x.Id)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文