2016-12-06 189 views
1

私の家族のビジネスに役立つ小さなウェブサイトを作成しようとしています。IEnumerableからIDを取得する必要があります<item>

私は2つのモデル、ジョブと日を持っています。

public class Job 
{ 
    public int JobId { get; set; } 
    public int LotNum { get; set; } 
    public string Street { get; set; } 
    public string Suburb { get; set; } 
    public double PerHour { get; set; } 
    public string Description { get; set; } 

} 

public class Day 
{ 
    public int JobId { get; set; } 
    public int DayId { get; set; } 
    public DateTime Date { get; set; } 
    public double Hours { get; set; } 
    public string Details { get; set; } 
    public int Invoice { get; set; } 
} 

私は請求書を作成する必要があり、請求書には番号が付けられます。日は請求書番号を持っているので、必要な日数を選ぶことができます。 これはこのように見えます。私は特定の請求書番号は、この使用して日を取得することができています

Date LotNum Street  Suburb  Hours 
    1/1/01 1 John Street Hurstville x hours 
    1/1/01 1 John Street Hurstville x hours 
    1/1/01 1 John Street Hurstville x hours 
    1/1/01 1 John Street Hurstville x hours 

:私はその日の日付と時間を持っていますが、そうすることによって

vm.Days = _dayRepo.GetAll().Where(d => d.Invoice == id); 

は、今私は、ジョブ情報を取得する必要があります。 DayとJobの両方がJobIdを持っているので、私はそれらをリンクすることができますが、私はちょうど方法を知らない。

これは私がこれまで持っているものです。

public IActionResult Invoice(int id) 
    { 
     CreateInvoiceViewModel vm = new CreateInvoiceViewModel(); 
     vm.Days = _dayRepo.GetAll().Where(d => d.Invoice == id); 
     //vm.JobId = 
     vm.Jobs = _jobRepo.GetAll(); 

     return View(vm); 
    } 

私の見解は次のようになります。私は、foreachのために入れるために何を知らない

@model CreateInvoiceViewModel 


<table> 

    @foreach (var item in Model.) 

    { 
      <tr> 
       <td>@item.Date.Date.ToString("dd/MM/yy")</td> 
       <td>@item.Hours</td> 


      </tr> 
    } 

</table> 

ありがとうございます!

+0

あなたはどのようなORMを使用していますか?あなたのレポジトリの日クラスも共有できますか? –

+0

請求書はジョブ間で共有されていますか?請求書番号がジョブエンティティに属するかのように見えます。 –

答えて

3

あなたはちょうど結合クエリが必要です。以下のようなあなたのViewModelを定義します。

public class InvoiceViewModel 
{ 
    public DateTime Date { get; set; } 

    public int LotNum { get; set; } 

    public string Street { get; set; } 

    public string Suburb { get; set; } 

    public double Hours { get; set; } 
} 

参加クエリを作成し、ビューモデルに変換します

public IActionResult Invoice(int id) 
{ 
    var query = from d in _dayRepo.GetAll() 
          join job in _jobRepo.GetAll() on d.JobId equals job.JobId 
          select new { Date=d.Date, LotNum= job.job , Street =job.Street , Suburb =job.Suburb , Hours =d.Hours }; 

    IEnumerable<InvoiceViewModel> viewModel = query.Select(c => new InvoiceViewModel() 
    { 
    Date=query.Date, 
    LotNum=query.LotNum, 
    Street=query.Street, 
    Suburb=query.Suburb, 
    Hours=query.Hours 
    }); 

    return View(viewModel); 
} 
+0

コントローラでこれをどうするかわかりません。しかし、見た目はしっかり! –

+0

@ YanniAlevras必要なフィールドを含むデータモデルを作成し、このクエリをモデルに変換し、 'Invoice'コントローラの戻り値の型をモデルに変更し、それをビューにバインドする必要があります。 – esiprogrammer

+0

DataModelはViewModelですか?どういうわけか、あなたがコードで何を意味しているのかという大まかなアイデアを私に見せていただければ幸いです。 –

1

これは、あなたがリストとしてそれをしたい場合にも、最後に.ToList()を追加することができ、あなたのID

var ids = _dayRepo.GetAll().Where(d => d.Invoice == id).Select(x => x.JobId); 

とIEnumerableを生じるはずです。

0

これは私がそれを設定する方法を多かれ少なかれある、またはどのように少なくとも始まり。あなたのコンテキストで

(私はEntityFrameworkCoreを使用しています、はい、私はあなたが現時点でレポを使用していることを理解する)

public class Job 
{ 
    public int Id { get; set; } 
    public int LotNum { get; set; } 
    public string Street { get; set; } 
    public string Suburb { get; set; } 
    public double PerHour { get; set; } 
    public string Description { get; set; } 

    // Navigation Helper 
    public virtual ICollection<InvoiceJobRelationship> Invoices { get; set; } 
} 

public class Day 
{ 
    public int Id { get; set; } 
    public DateTime Date { get; set; } 
    public double Hours { get; set; } 
    public string Details { get; set; } 

    // Navigation Helper 
    public virtual ICollection<InvoiceDayRelationship> Invoices { get; set; } 
} 

public class Invoice 
{ 
    public int Id { get; set; } 

    // Navigation Helpers 
    public virtual ICollection<InvoiceJobRelationship> Jobs { get; set; } 
    public virtual ICollection<InvoiceDayRelationship> Days { get; set; } 
} 
public class InvoiceDayRelationship 
{ 
    public int InvoiceId { get; set; } 
    // Navigation Helper 
    [ForeignKey("InvoiceId")] 
    public Invoice Invoice { get; set; } 
    public int DayId { get; set; } 
    // Navigation Helper 
    [ForeignKey("DayId")] 
    public Day Day { get; set; } 
} 
public class InvoiceJobRelationship 
{ 
    public int InvoiceId { get; set; } 
    // Navigation Helper 
    [ForeignKey("InvoiceId")] 
    public Invoice Invoice { get; set; } 
    public int JobId { get; set; } 
    // Navigation Helper 
    [ForeignKey("JobId")] 
    public Job Job { get; set; } 
} 

その後

protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 

     builder.Entity<InvoiceDayRelationship>() 
      .HasKey(id => new { id.DayId, id.InvoiceId }); 
     builder.Entity<InvoiceJobRelationship>() 
      .HasKey(ij => new { ij.JobId, ij.InvoiceId }); 
    } 

    public DbSet<Invoice> Invoices { get; set; } 
    public DbSet<Day> Days { get; set; } 
    public DbSet<Job> Jobs { get; set; } 
    public DbSet<InvoiceDayRelationship> InvoiceDays { get; set; } 
    public DbSet<InvoiceJobRelationship> InvoiceJobs { get; set; } 

次に、あなたはかなりを呼び出すことができるだろうあなたが好きなもの

(クエリ例)

(from x in context.Invoices where x.Id == id select x).Include(inv => inv.Days).Include(inv => inv.Jobs); 
関連する問題