Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
C# 获取表值加上同一linq语句中某些列的计数_C#_Linq - Fatal编程技术网

C# 获取表值加上同一linq语句中某些列的计数

C# 获取表值加上同一linq语句中某些列的计数,c#,linq,C#,Linq,大家好,我正在使用一个Dto类对象,如下所示 public class TypeDTO{ public long Type { get; set; } public string Name { get; set; } public byte DefType { get; set; } public byte DefCode { get; set; } public Dictionary<int,deci

大家好,我正在使用一个Dto类对象,如下所示

    public class TypeDTO{

       public long Type { get; set; }

       public string Name { get; set; }

       public byte DefType { get; set; }

       public byte DefCode { get; set; }

       public Dictionary<int,decimal> SumOfEachType = new Dictionary<int,decimal>();
      }
      SELECT rit_type,count(rit_type) as sum
      FROM table group by rit_type
在SumOfEachType属性中,我希望保存sql查询的结果,如下所示

    public class TypeDTO{

       public long Type { get; set; }

       public string Name { get; set; }

       public byte DefType { get; set; }

       public byte DefCode { get; set; }

       public Dictionary<int,decimal> SumOfEachType = new Dictionary<int,decimal>();
      }
      SELECT rit_type,count(rit_type) as sum
      FROM table group by rit_type
到目前为止可能的方法:

1) 创建2个linq语句。按照这种方式,我不确定如何将结果合并到一个typeDTO对象中

2) 使用一个linq查询,当SumOfEachType需要获取一个未知的值时,到目前为止,我包括了第二个linq语句来获取计数和rit\u type


感谢

因此,考虑到以下数据库模型和dto:

public class DatabaseModel
{
    public int RIT_TYPE { get; set; }
    public string RIT_NAME { get; set; }
    public int RIT_DEF_TYPE { get; set; }
    public int RIT_DEF_CODE { get; set; }
}

public class TypeDTO
{
    public long Type { get; set; }

    public string Name { get; set; }

    public byte DefType { get; set; }

    public byte DefCode { get; set; }

    public Dictionary<int, int> SumOfEachType = new Dictionary<int, int>();
}

因此,给定以下数据库模型和dto:

public class DatabaseModel
{
    public int RIT_TYPE { get; set; }
    public string RIT_NAME { get; set; }
    public int RIT_DEF_TYPE { get; set; }
    public int RIT_DEF_CODE { get; set; }
}

public class TypeDTO
{
    public long Type { get; set; }

    public string Name { get; set; }

    public byte DefType { get; set; }

    public byte DefCode { get; set; }

    public Dictionary<int, int> SumOfEachType = new Dictionary<int, int>();
}

为什么要将每个类型的摘要放在每个单独的对象中?使用两个单独的查询和两个单独的数据是否可以构造查询,以便使用
.ToDictionary(ds=>ds.ItemAsKey,ds=>ds.ItemAsValue)
?Karl我想你的意思是一个linq将包含.ToDictionary,然后正如我上面所说的,我将尝试将两个linq结果合并到一个返回ienumerable?这就是我不知道如何处理的问题:)为什么要将每种类型的摘要放入每个单独的对象中?使用两个单独的查询和两个单独的数据是否可以构造查询,以便使用
.ToDictionary(ds=>ds.ItemAsKey,ds=>ds.ItemAsValue)
?Karl我想你的意思是一个linq将包含.ToDictionary,然后正如我上面所说的,我将尝试将两个linq结果合并到一个返回ienumerable?这就是我不知道如何解决的问题:)当我看到它时,我几乎可以肯定它会工作……但是当我使用api控制器的数据时,我得到以下错误. ..在此上下文中只支持基元类型或枚举类型…您有什么想法吗?对不起,伙计!我浏览了一遍,遗漏了
\u db.table
\u db
作为您的上下文。所以我认为问题在于EF不知道如何处理作为select语句一部分的投影字典。因此,如果您将
\u db.table.aseneumerable().Select(model=>newtypedto
放入,则它应该可以工作。这意味着您正在将整个表拉入内存中,因此必须使用aseneumerable()小心。这就是IQueryable vs IEnumerable发挥作用的地方。当我看到它时,我几乎可以肯定它会起作用……但是当我使用api控制器的数据时,我得到了以下错误……在这个上下文中只支持基元类型或枚举类型……你有什么想法吗?对不起,伙计!我浏览了一下,错过了
\u db.table
\u db
是您的上下文。因此,我认为问题是EF不知道如何处理作为select语句一部分的投影字典。因此,如果您将
\u db.table.AsEnumerable()放入(model=>newtypedto
那么它应该可以工作。这意味着你要把整个表放到内存中,所以你必须小心地使用AsEnumerable()。这就是IQueryable和IEnumerable的作用。
        var datebaseModelDictionary = databaseModels
            .GroupBy(model => model.RIT_TYPE)
            .ToDictionary(models => models.Key, models => models.Count());

        var result = databaseModels
            .Select(model => new TypeDTO
            {
            Type = long.Parse(model.RIT_TYPE.ToString()),
            Name = model.RIT_NAME,
            DefType = byte.Parse(model.RIT_DEF_TYPE.ToString()),
            DefCode = byte.Parse(model.RIT_DEF_CODE.ToString()),
            SumOfEachType = datebaseModelDictionary
        });