2017-12-13 14 views
0

私は次のモデルがあります:EFコアの追加フィールドでSQL SELECTクエリを実行するにはどうすればよいですか?

[Table("clients")] 
    public class ClientDto : BaseModelDto 
    { 
     [Required] 
     [StringLength(70, MinimumLength = 3)] 
     [Column("fio")] 
     public string FIO { get; set; } 

     [StringLength(100, MinimumLength = 3)] 
     [Column("address")] 
     public string Address { get; set; } 

     [Phone] 
     [Column("phone")] 
     public string Phone { get; set; }   

     public List<RealizationDto> Realizations { get; set; } 
    } 

をそして私は、SQLクエリを実行し、結果を取得する必要がありますので、PostgreSQLで動作しますが、サーバーレベルでは動作しません

public async Task<ClientDto> GetTheMostValuableCustomer() 
    { 
     var result = await Context.Clients.FromSql<ClientDto>(@" 
     select clients.id, clients.fio, clients.address, clients.phone, sum(goods.price*realizations.quantity) max_cost 
      from clients 
      inner join realizations on clients.id=realizations.client_id 
      inner join goods on realizations.good_id=goods.id 
      group by clients.id 
      order by max_cost desc 
     limit 1; 
    ").ToListAsync(); 

    return result.FirstOrDefault(); 
} 

このquesryをClientDtoのモデルと選択フィールドは異なります。 (max_costはClientDtoモデルには存在しません)。しかし、クエリを実行すると、フィールド "max_cost"が存在しないというエラーが表示されます。どうすればこの問題を解決できますか?

、変更する必要が
+1

あなたの代わりに選択リストの順に 'max_cost'を移動する場合はどう?。例えば'orders by sum(goods.price * realizations.quantity) ' –

+0

どのデータベースプロバイダですか? –

+0

@Gert Arnold PostgreSQL – user190794

答えて

0

sum(goods.price*realizations.quantity) max_cost 

へ:

sum(goods.price*realizations.quantity) AS max_cost 
+0

私はこれらの構造が等しいと思います – user190794

関連する問題