迭代 LINQ AnonymousType 对象

发布于 2024-12-11 13:45:49 字数 1033 浏览 0 评论 0原文

如何在另一个方法中使用此 LINQ 的结果并获取属性 CountryIDcount

public IQueryable GetRequestsByRegion(string RequestType)
{
        try
        {
            var q = from re in context.RequestExtensions
                    from r in context.Requests
                    where re.ExtensionID == r.ExtraInfoID
                    && r.OriginalRequestID == null
                    && r.RequestType == RequestType
                    group re by new { CountryID = re.CountryID } into grouped
                    select new { CountryID = (int)grouped.Key.CountryID, count = (int)grouped.Count(t => t.CountryID != null) } ;

            return q;
        }
        catch (Exception ex)
        {

        }

        return null;

    }

public void GetAllInformationRequestsByRegion()
    {
        IQueryable dict = GetRequestsByRegion("tab8elem1");

        /* How to iterate and get the properties here? */

    }

返回类型和变量类型不需要是指示的类型......这只是我的尝试。我也在使用 WCF,因此无法返回对象类型。

How to use the result of this LINQ in another method and get the properties CountryID and count?

public IQueryable GetRequestsByRegion(string RequestType)
{
        try
        {
            var q = from re in context.RequestExtensions
                    from r in context.Requests
                    where re.ExtensionID == r.ExtraInfoID
                    && r.OriginalRequestID == null
                    && r.RequestType == RequestType
                    group re by new { CountryID = re.CountryID } into grouped
                    select new { CountryID = (int)grouped.Key.CountryID, count = (int)grouped.Count(t => t.CountryID != null) } ;

            return q;
        }
        catch (Exception ex)
        {

        }

        return null;

    }

public void GetAllInformationRequestsByRegion()
    {
        IQueryable dict = GetRequestsByRegion("tab8elem1");

        /* How to iterate and get the properties here? */

    }

The return types and variable types don't need to be the ones indicated... This was just my try. I am also using WCF so I can't return Object types.

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

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

发布评论

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

评论(4

何时共饮酒 2024-12-18 13:45:49

就像它是任何其他类型的对象一样:

foreach(var obj in q) {
    var id = obj.CountryID;
    var count = obj.count;
}

Just like as if it were any other kind of object:

foreach(var obj in q) {
    var id = obj.CountryID;
    var count = obj.count;
}
梦醒时光 2024-12-18 13:45:49

附加

也许您想在方法之外使用它?然后使用类似这样的东西:

public void ForEach<T>(IEnumerable<T> l, Action<T> a)
{
  foreach (var e in l) a(e);
}

用法:

ForEach(from x in bar select new { Foo = x, Frequency = 4444, Pitch = 100 }, 
  x => 
  { 
    //do stuff here
    Console.WriteLine(x.Foo);
    Console.Beep(x.Pitch,x.Frequency);
  });

Additional

Perhaps you want to use this outside the method? Then use something like this:

public void ForEach<T>(IEnumerable<T> l, Action<T> a)
{
  foreach (var e in l) a(e);
}

Usage:

ForEach(from x in bar select new { Foo = x, Frequency = 4444, Pitch = 100 }, 
  x => 
  { 
    //do stuff here
    Console.WriteLine(x.Foo);
    Console.Beep(x.Pitch,x.Frequency);
  });
没有心的人 2024-12-18 13:45:49

您可以像对待常规 C# 对象一样对待结果。 Intellisense 将帮助您完成匿名输入。

foreach (var anonymousObject in q)
{
    // anonymousObject.CountryID;
    // anonymousObject.count;
}

You can treat the result as you would a regular C# object. Intellisense will help you out with the anonymous typing.

foreach (var anonymousObject in q)
{
    // anonymousObject.CountryID;
    // anonymousObject.count;
}
尝蛊 2024-12-18 13:45:49

匿名类型对于声明它们的方法来说是本地的。您不能直接返回它们。如果您需要将它们公开在声明方法之外,则需要投影到您可以命名的类型(您自己的自定义类或其他现有框架类,如 KeyValuePair)。

Anonymous types are local to the method that they are declared in. You can't return them directly. If you need them to be exposed outside of the declaring method, you need to project into a type that you can name (either your own custom class or some other existing framework class like KeyValuePair).

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