2017-08-30 37 views
-1

2つのテーブルからデータを選択する際に問題があります。ここで私は、私は次のエラーを取得する新しい選択すると、私のコード複数のテーブルから選択linq Web API

var results = (from voter in context.Voters 
          join voter2 in context.PollingShehias on voter.PollingStationId equals voter2.PollingStationId 
          join poll in context.PollingStations on voter.PollingStationId equals poll.Id 
          where voter2.WordId == Id 
          orderby voter.FirstName, voter.MiddleName, voter.LastName, voter.Gender ascending 
          select new 
          { 
           voter.Id, 
           voter.FirstName, 
           voter.MiddleName, 
           voter.LastName, 
           voter.DateBirth, 
           voter.Gender, 
           voter.ResidentialAddress, 
           poll.Name, 
           voter.VoterIDNumber, 
           voter.LifeStatus 
          }); 

return results; 

ある

ExceptionMessage「:」「Cuf.infrastructure.Models.Voter」を入力するタイプ「匿名タイプ」をキャストすることができません。

+0

匿名のタイプを選択しています。おそらく、この方法は '投票者 '型を返すことを期待していますか?そのタイプを選択するか、結果からそのタイプの新しいインスタンスを作成できますか?あなたは何をしようとしているのですか?メソッドは何を返すべきですか? – David

+0

メソッドの戻り値のIEnumerable GetVoteer(int型のID)が – user1554459

答えて

0

select voter

select new { ... }

を交換してください。しかし、この場合には、あなたはpoll.Nameを返すことはできません。おそらく、PollNameプロパティを持つ新しいクラスを作成し、IEnumerable<Voter>の代わりにIEnumerableを返す必要があります。

例:投票はクラス(モデル)名

ある

public class VoterWithPollName { 
    public long Id { get; set; } 
    public sitrng FirstName { get; set; } 
    public string MiddleName { get; set; } 
    public string LastName { get; set; } 
    public DateTime DateBirth { get; set; } 
    public sting Gender { get; set; } 
    public string ResidentialAddress { get; set; } 
    public string PollName { get; set; } 
    public long VoterIDNumber { get; set; } 
    public string LifeStatus { get; set; } 
} 

... 

// set method return type to IEnumerable<VoterWithPollName> 

var results = (from voter in context.Voters 
          join voter2 in context.PollingShehias on voter.PollingStationId equals voter2.PollingStationId 
          join poll in context.PollingStations on voter.PollingStationId equals poll.Id 
          where voter2.WordId == Id 
          orderby voter.FirstName, voter.MiddleName, voter.LastName, voter.Gender ascending 
          select new VoterWithPollName 
          { 
           Id = voter.Id, 
           FirstName = voter.FirstName, 
           MiddleName = voter.MiddleName, 
           LastName = voter.LastName, 
           DateBirth = voter.DateBirth, 
           Gender = voter.Gender, 
           ResidentialAddress = voter.ResidentialAddress, 
           PollName = poll.Name, 
           VoterIDNumber = voter.VoterIDNumber, 
           LifeStatus = voter.LifeStatus 
          }); 

return results; 
+0

poll.Nameが、私は問題 – user1554459

+0

はBTW例 –

+0

を追加されますされた場合、 'VoterIDNumber'は、それが働いたガイドライン –

0

ですから、コード

voter in context.Voters 

を書く有権者がモデルまたはクラス名ですが、今回は、オブジェクト

です

ただし、クラス名とオブジェクト名は同じ匿名型例外

だから、あなたは以下の方法

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    base.OnModelCreating(modelBuilder); 
} 
をオーバーライドし、あなたのMYDBのコンテキストクラスでのコードの記述

後、複数のテーブル名の問題

に回避するために、どのように有権者の変更投票、または任意の名前

を持っています

+0

あなたの助けをありがとう – user1554459

関連する問題