简体   繁体   English

C# 如何用 IQueryable 定义匿名类型?

[英]C# how to define an anonymous type with IQueryable?

Depending on a switch/case a variable is set to an EF chaining.根据开关/情况,变量设置为 EF 链接。 but I cannot figure how to define the variable groupByLeadIdQuery但我不知道如何定义变量groupByLeadIdQuery

IQueryable groupByLeadIdQuery = null;

switch (...)
{
    case ...:
    {
         groupByLeadIdQuery = context.HistoryRecords.GroupBy(h => h.ProductId)
         .Select(g => new
         {
              Id = g.Key,
              Retributions = g.Sum(s => s.Retribution),
         });
    }
    break;
    case ...:
    {
        groupByLeadIdQuery = null;
    }
    ...
}

later in the code, I am processing the results depending on the variable is null or not稍后在代码中,我根据变量是否为 null 来处理结果

if (groupByLeadIdQuery2 != null)
{
    var list = groupByLeadIdQuery2.ToList(); <<<<<<<<<<<<<<<<<<
    ...
}

the compiler complains: IQueryable does not contain a definition of ToList编译器抱怨: IQueryable 不包含 ToList 的定义

I tried我试过了

IQueryable<T> groupByLeadIdQuery = null;
IQueryable<Anonymous> groupByLeadIdQuery = null;
IQueryable<'a> groupByLeadIdQuery = null;
var groupByLeadIdQuery = null;

nothing works.没有任何作用。

can you enlight me on this?你能启发我吗?

thanks for your help感谢您的帮助

For having ToList method you should have define your IQueryable variable as List.要拥有 ToList 方法,您应该将 IQueryable 变量定义为 List。 For example IQueryable<Product> or something Like this.例如IQueryable<Product>或类似的东西。 If there is not a certain Type you can use just var and get your anonymous type.如果没有特定类型,您可以只使用 var 并获取您的匿名类型。

In your case I think easiest solution will be setting a default value and than updating by cases.在您的情况下,我认为最简单的解决方案是设置默认值,而不是按情况更新。 But in this case you have to set same anonymous type for each cases, otherwise you have to cast types.但是在这种情况下,您必须为每种情况设置相同的匿名类型,否则您必须强制转换类型。

 var groupByLeadIdQuery = context.HistoryRecords.GroupBy(h => h.ProductId)
     .Select(g => new
     {
          Id = g.Key,
          Retributions = g.Sum(s => s.Retribution),
     });
switch (...)
{
case ...:
{
     groupByLeadIdQuery = ...;
}
break;
case ...:
{
    groupByLeadIdQuery = null;
}
...
}

If this is not solving your problem you should use interfaces, an interface which contains the properties you need, or dynamics maybe.如果这不能解决您的问题,您应该使用接口,一个包含您需要的属性的接口,或者可能是动态。

Switch expressions may be a good option here.切换表达式在这里可能是一个不错的选择。 The fundamental problem is that you need the <T> in IQueryable<T> , but it is "unpronounceable", so you can only use var to declare such, but:根本问题是您需要IQueryable<T> <T>中的 <T> ,但它是“不可发音的”,因此您只能使用var来声明它,但是:

var query = whatever switch {
    SomeCase => your.Query.Here...,
    _ => null,
};

Note that while you can have as many case lines as you need, they must all have the same shape for this to work.请注意,虽然您可以根据需要拥有尽可能多的案例行,但它们必须具有相同的形状才能正常工作。

You can make generic Group By method some thing like this:您可以使通用Group By方法如下所示:

 public async Task<List<T>> GetGroupByResult<T>(string groupByItem) where T : class
        {
            var groupByLeadIdQuery = _dbContext.Set<T>();

            switch (groupByItem)
            {
                case "ProductId":
                {
                        groupByLeadIdQuery = groupByLeadIdQuery.GroupBy(h => h.ProductId)
                            .Select(g => new
                            {
                                Id = g.Key,
                                Retributions = g.Sum(s => s.Retribution),
                            });

                        break;
                }
                case "Other":
                {
                    groupByLeadIdQuery = ...;

                    break;
                }
                default:
                    {
                        groupByLeadIdQuery = null;
                    }
            }

            if (groupByLeadIdQuery != null)
                return await groupByLeadIdQuery.ToListAsync();

            return default;
        }

It can use for any of your entities.它可以用于您的任何实体。

from King King来自大王

groupByLeadIdQuery2.Cast<dynamic>().ToList() 

works like a charm奇迹般有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM