SQL查询与group by and and efference the接收参数

发布于 2025-02-02 21:37:40 字数 330 浏览 4 评论 0原文

我正在尝试在linq ef中做到这一点我的SQL查询:

SELECT date FROM tab_4009_atv
WHERE id_asset IN ['ako','bj89','flity76']
GROUP BY date; 

但是有一个特定的情况。 我要过滤的列表是作为这样的参数收到的:

public async Task<IEnumerable< Date>> getDates (IEnumerable< string>assetList){ }.

我该怎么做?谢谢!

I'm trying to do in Linq EF this my sql query:

SELECT date FROM tab_4009_atv
WHERE id_asset IN ['ako','bj89','flity76']
GROUP BY date; 

but there's a specific situation.
This list that I'm filtering is received as a parameter like this:

public async Task<IEnumerable< Date>> getDates (IEnumerable< string>assetList){ }.

How can I do this? Thanks!

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

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

发布评论

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

评论(1

轻许诺言 2025-02-09 21:37:40

您给出的SQL查询和C#方法签名并不真正适合彼此。 SQL查询返回一个日期分组,但是C#方法返回日期列表。我们需要修改该方法的返回类型。

另一方面,与SQL或LINQ相比,这个问题更多是要将字符串转换为Asset对象。由于我们不知道Asset对象看起来像我们无法帮助您进行转换。我们必须将这一责任委托给另一种方法。

此外,该方法不需要异步。

以下代码应执行您要寻找的内容。

public class Asset
{
    public DateTime Date { get; set; }
    public string? OtherData { get; set; }
}

private static Asset StringToDate(string assetString)
{
    // Convert string to asset object
}

public IEnumerable<IGrouping<DateTime, Asset>> GetDates(IEnumerable<string> assetList)
{
    var filter = new[] {"ako", "bj89", "flity76"};
    
    var dates = assetList
        .Where(x => filter.Contains(x))
        .Select(x => StringToDate(x))
        .GroupBy(assets => assets.Date);

    return dates;
}

The SQL query and the C# method signature you have given don't really fit each other. SQL query returns a grouping of dates, but the C# method returns a list of dates. We need to modify the return type of the method.

On the other hand, this question is more about converting the string to the Asset object than SQL or LINQ. Since we do not know what the Asset object looks like we cannot help you with that conversion. We have to delegate that responsibility to another method.

Additionally, the method doesn't need to be async.

The following code should do what you are looking for.

public class Asset
{
    public DateTime Date { get; set; }
    public string? OtherData { get; set; }
}

private static Asset StringToDate(string assetString)
{
    // Convert string to asset object
}

public IEnumerable<IGrouping<DateTime, Asset>> GetDates(IEnumerable<string> assetList)
{
    var filter = new[] {"ako", "bj89", "flity76"};
    
    var dates = assetList
        .Where(x => filter.Contains(x))
        .Select(x => StringToDate(x))
        .GroupBy(assets => assets.Date);

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