在查询中使用输出参数的 .NET LINQ 调用方法并使用输出值

发布于 2024-09-25 05:28:08 字数 129 浏览 5 评论 0原文

我有一个对象列表,其中有一个带有几个输出参数的方法。我如何在每个对象上调用此方法,获取输出参数值并稍后在查询中使用它们,也许用于检查 where 子句?

这是否可能,如果可以,请有人通过示例代码进行演示。

谢谢!

I have a list of objects, which has a method that has a couple of out parameters. How do i call this method on each object, get the out parameter values and use them later on in the query, perhaps for checking in a where clause?

Is this possible and if so can someone please demonostrate through sample code.

Thanks!

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

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

发布评论

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

评论(5

请叫√我孤独 2024-10-02 05:28:08

以下是访问 LINQ 查询中的输出参数值的一种方法。我不认为您可以在以后的选择中使用来自 where 的输出值: list.Where(...).Select(...)

List<MyClass> list; // Initialize

Func<MyClass, bool> fun = f =>
{
    int a, b;
    f.MyMethod(out a, out b);
    return a == b;
};
list.Where(fun);

MyClass 的实现是这样的;

public class MyClass
{
    public void MyMethod(out int a, out int b)
    {
        // Implementation
    }
}

Here is one way of accessing the values of out parameters in your LINQ query. I dont think that you can use the out-values from say a where in a later select: list.Where(...).Select(...)

List<MyClass> list; // Initialize

Func<MyClass, bool> fun = f =>
{
    int a, b;
    f.MyMethod(out a, out b);
    return a == b;
};
list.Where(fun);

Where MyClass is implemented something like this;

public class MyClass
{
    public void MyMethod(out int a, out int b)
    {
        // Implementation
    }
}
爱,才寂寞 2024-10-02 05:28:08

也许您应该使用 for every 循环,然后使用您的查询?

(实际上,在不知道你的代码的情况下很难说出在这种情况下该怎么做最好)

Maybe you should use a for each loop and then use your query?

(Actually, it's hard to say what to do best in this situation without knowing your code)

你是年少的欢喜 2024-10-02 05:28:08

这使用 Tuple来自 .NET 4.0,但可以适用于早期版本:

//e.g., your method with out parameters
void YourMethod<T1,T2,T3>(T1 input, out T2 x, out T3 y) { /* assigns x & y */ }

//helper method for dealing with out params
Tuple<T2,T3> GetTupleOfTwoOutValues<T1,T2,T3>(T1 input)
{ 
   T2 a;
   T3 b;
   YourMethod(input, out a, out b);
   return Tuple.Create(a,b);
}

IEnumerable<Tuple<T2,T3>> LinqQuery<T1,T2,T3>(IEnumerable<T1> src, T2 comparisonObject)  
{
   return src.Select(GetTupleOfTwoOutValues)
             .Where(tuple => tuple.Item1 == comparisonObject);
}

This uses Tuple<T1,T2> from .NET 4.0, but can be adapted for earlier versions:

//e.g., your method with out parameters
void YourMethod<T1,T2,T3>(T1 input, out T2 x, out T3 y) { /* assigns x & y */ }

//helper method for dealing with out params
Tuple<T2,T3> GetTupleOfTwoOutValues<T1,T2,T3>(T1 input)
{ 
   T2 a;
   T3 b;
   YourMethod(input, out a, out b);
   return Tuple.Create(a,b);
}

IEnumerable<Tuple<T2,T3>> LinqQuery<T1,T2,T3>(IEnumerable<T1> src, T2 comparisonObject)  
{
   return src.Select(GetTupleOfTwoOutValues)
             .Where(tuple => tuple.Item1 == comparisonObject);
}
献世佛 2024-10-02 05:28:08

您可以使用匿名对象和 let 关键字:

var texts = new[] { "dog", "2", "3", "cat" };
var checks = from item in texts
             let check = new
             {
                 Word = item,
                 IsNumber = int.TryParse(item, out var n),
                 Value = n,
             }
             where check.IsNumber
             select check;
foreach(var item in checks) 
{
    Console.WriteLine($"'{item.Word}' is the number {item.Value}");
}

You could use anonymous objects and the let keyword:

var texts = new[] { "dog", "2", "3", "cat" };
var checks = from item in texts
             let check = new
             {
                 Word = item,
                 IsNumber = int.TryParse(item, out var n),
                 Value = n,
             }
             where check.IsNumber
             select check;
foreach(var item in checks) 
{
    Console.WriteLine(
quot;'{item.Word}' is the number {item.Value}");
}
世俗缘 2024-10-02 05:28:08

您可以使用元组(无需任何辅助方法):

var text = "123,456,abc";
var items = text.Split(',')
    .Select(x => (long.TryParse(x, out var v), v))
    .Where(x => x.Item1)
    .Select(x => x.Item2);

foreach (var item in items)
{
    Console.WriteLine(item);
}

输出

123
456

本文有一些其他解决方案: https:/ /mydevtricks.com/linq-gems-out-parameters

You can use tuples (without any helper methods):

var text = "123,456,abc";
var items = text.Split(',')
    .Select(x => (long.TryParse(x, out var v), v))
    .Where(x => x.Item1)
    .Select(x => x.Item2);

foreach (var item in items)
{
    Console.WriteLine(item);
}

Output

123
456

This article has some additional solutions: https://mydevtricks.com/linq-gems-out-parameters

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