2016-12-06 38 views
1

所以即時嘗試創建一個小網站來幫助我的家人的業務。需要從IEnumerable獲得ID <item>

我有兩個模型,工作和天。

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); 
    } 

我的看法是這樣的:

@model CreateInvoiceViewModel 


<table> 

    @foreach (var item in Model.) 

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


      </tr> 
    } 

</table> 

我不知道要放什麼東西在對foreach。

在此先感謝!

+0

你使用什麼樣的ORM?你還可以分享你的知識庫日課嗎? –

+0

作業之間是否共享發票?它看起來好像發票號碼應該屬於工作實體。 –

回答

3

你只需要一個連接查詢。定義你的視圖模型,如:

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你必須創建一個DataModel的,其中包括您所需的字段和查詢轉換爲'Invoice'控制器的型號和改變返回值類型的模型,並將其綁定到您的視圖 – esiprogrammer

+0

通過的DataModel你的意思是視圖模型?我將不勝感激,如果你能夠以某種方式向我展示你的代碼意思。 –

1

這將導致一個IEnumerable與IDS

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

您還可以,如果你想把它當作一個列表中添加.ToList()到底。

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); 
相關問題