Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework core 如何在没有自定义sql的情况下在EFCore 3.1中选择常量值_Entity Framework Core_Iqueryable - Fatal编程技术网

Entity framework core 如何在没有自定义sql的情况下在EFCore 3.1中选择常量值

Entity framework core 如何在没有自定义sql的情况下在EFCore 3.1中选择常量值,entity-framework-core,iqueryable,Entity Framework Core,Iqueryable,我希望EFCore从表中生成等价的“选择名称、颜色、‘notapplicatable’作为位置” 我在一个上下文中有多个实体,我需要将IQueryable传递给一个将GroupBy(x=>x.Position)的通用函数。如果我将Position标记为NotMapped,GroupBy将不会在服务器端执行。我尝试了各种组合,试图使用ValueGenerateOn…和ComputedSql,但都没有成功 在下面的示例中,ActivityWithRealQty有一个配置,将所有3个字段映射到数据库中

我希望EFCore从表中生成等价的“选择名称、颜色、‘notapplicatable’作为位置”

我在一个上下文中有多个实体,我需要将IQueryable传递给一个将GroupBy(x=>x.Position)的通用函数。如果我将Position标记为NotMapped,GroupBy将不会在服务器端执行。我尝试了各种组合,试图使用ValueGenerateOn…和ComputedSql,但都没有成功

在下面的示例中,ActivityWithRealQty有一个配置,将所有3个字段映射到数据库中的列,但ActivityWithStaticQty在数据库中没有一个字段表示数量。但是,我希望它返回1作为静态值,以便GroupBy在服务器端正常工作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    public class Class1
    {
        private IEnumerable<ResultActivity> GroupData(IQueryable<IActivity> data)
        {
            var rtn = data.GroupBy(result => new
            {
                result.Name,
                result.Title
            })
            .Select(x => new ResultActivity()
            { 
                Name = x.Key.Name,
                Title = x.Key.Title,
                Quantity = x.Sum(n => n.Quantity)
            })
            .ToList();

            return rtn;
        }
    }

    public class ResultActivity
    {
        public ResultActivity()
        {
        }

        public string Name { get; set; }
        public string Title { get; set; }

        public int Quantity { get; set; }
    }

    public interface IActivity
    {
        public string Name { get; set; }
        public string Title { get; set; }

        public int Quantity { get; set; }
    }

    public class ActivityWithStaticQty : IActivity
    {
        public string Name { get ; set ; }
        public string Title { get; set; }
        public int Quantity { get; set; } = 1;
    }

    public class ActivityWithRealQty : IActivity
    {
        public string Name { get; set; }
        public string Title { get; set; }
        public int Quantity { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间控制台EAPP1
{
公共班级1
{
私有IEnumerable组数据(IQueryable数据)
{
var rtn=data.GroupBy(结果=>new
{
结果.名称,
结果.标题
})
.Select(x=>newresultivity()
{ 
Name=x.Key.Name,
Title=x.Key.Title,
数量=x.总和(n=>n.数量)
})
.ToList();
返回rtn;
}
}
公共类结果性
{
公共结果性()
{
}
公共字符串名称{get;set;}
公共字符串标题{get;set;}
公共整数数量{get;set;}
}
公共接口活动性
{
公共字符串名称{get;set;}
公共字符串标题{get;set;}
公共整数数量{get;set;}
}
公共课堂活动与活动:IActivity
{
公共字符串名称{get;set;}
公共字符串标题{get;set;}
公共整数数量{get;set;}=1;
}
具有realQuantity:IActivity的公共类活动
{
公共字符串名称{get;set;}
公共字符串标题{get;set;}
公共整数数量{get;set;}
}
}

我认为如果您向我们展示代码会有所帮助。请将一个示例作为您问题的一部分。@Mo Gang代码添加到原始postI guess
Gorubby
中在EFCore 3中不可用。1@RajdeepDebnath当您分组时,它运行在服务器端,由于该字段在表中不存在,它会失败“在生成的sql中使用静态值