2016-08-27 5 views
2

複数の請求書明細で請求書を作成しています。これは、請求書には、複数の請求書明細でどのように見えるかです: enter image description hereLinq - シーケンスに複数の要素が含まれています

私はページ全体の更新を行う際に、それが壊れると、このエラーがスローされます。

public ActionResult Create(long userId, long invoiceId) 
    { 
     ViewBag.InvoiceId = invoiceId; 
     ViewBag.UserId = userId; 
     ViewBag.UserId = userId; 
     var objInvoiceItems = (from i in db.tblinvoiceitems 
         where i.InvoiceId == invoiceId 
         select i 
       ).ToList(); 

     var a = (from o in objInvoiceItems 
       select new InvoiceItemViewModel { 
        Description = o.DESCRIPTION, 
         Quantity = o.Quantity, 
         Rate = o.Rate, 
         Id = o.ID, 
         InvoiceId = o.InvoiceId 
       }).ToList(); 

     var objInvoice = (from i in db.tblinvoices 
          where i.ID == invoiceId 
          select i 
       ).ToList(); 

     var obj = (from i in objInvoice 
        join j in a 
        on i.ID equals j.InvoiceId 
        select new InvoiceCreateViewModel 
        { 
         AmountPaid = i.AmountPaid, 
         DueDate = i.DueDate, 
         InvoiceNo = i.ID, 
         UserId = i.UserId, 
         InvoiceItems = a 
         //InvoiceItems = j 
        }).SingleOrDefault(); 
     //obj.InvoiceItems = ListInvoiceItems(invoiceId.ToString()).Model; 
     if (obj == null) 
     { 
      obj = new InvoiceCreateViewModel(); 
     } 
     return View(obj); 
    } 


    public class InvoiceCreateViewModel 
{ 
    public long? UserId { get; set; } 
    public double? AmountPaid { get; set; } 
    public DateTime? DueDate { get; set; } 

    public double? TotalAmount { get; set; } 
    public long InvoiceNo { get; set; } 
    public double? Rate { get; set; } 
    public string Description { get; set; } 
    public int? Quantity { get; set; } 

    public IEnumerable<InvoiceItemViewModel> InvoiceItems { get; set; } 

} 
:これは私のコードである

Sequence contains more than one element 

これは、ラインSingleOrDefault()で壊れています。請求書は常に1つで、InvoiceItemsは複数にすることができます。私はAdd new Invoice Itemsをjqueryを介して別の関数で処理していますので、この関数は請求書項目のない新しい請求書を作成すると一度しかヒットしません。

複数の請求書明細で1つの請求書を返送したいと思います。

+0

これをすべてのあなたのLINQクエリを行うことができます複数のinvoiceItemが存在する可能性があるため、デカルト積の結果です。あなたがしたいのは 'InvoiceItemViewModel'のリストを持つ' InvoiceCreateViewModel'を持っています –

+0

はい、どうしますか?私のinvoicecreateviewmodelでは、私はienumberable Baahubali

答えて

1

そう

@model ViewModelExample 

は、単に必要があります新しいモデルを作成するようにカミソリエンジンはあなたのビューの上部にあるのViewModelを宣言使用して呼び出すことができる単一のモデルにクラスを結合したい場合そう

modelExample.invoice = obj 
modelExample.itemsList= objInvoice 
のような請求書とデータ型への請求書の項目のリスト

public class ViewModelExample 
{ 
    public Invoice invoice{get; set;} 
    public List<InvoiceItems> itemsList{get; set;} 
} 

割り当てViewModelExample正しいオブジェクト

はその後modelExampleオブジェクトを返し、ViewModelExampleクラスで提供されるプロパティのそれぞれについて、結果を表示するためのRazerを使用

+0

thatsちょうど私がそれを持っています。しかし、どのようにSQLにlinqでそれを設定するのですか? – Baahubali

+1

'FirstOrDefault()'を 'FirstOrDefault()'に置き換えてください。まだシーケンスエラーが出たら教えてください –

+0

FirstorDefaultは – Baahubali

0

あなたはおそらく一度(少ないオーバーヘッド)で

var obj = (from invoice in db.tblinvoices 
       where invoice.ID == invoiceId 
       join items in db.tblinvoiceitems 
       on invoice.ID == items.InvoiceId 
       select new InvoiceCreateViewModel 
        { 
         AmountPaid = invoice.AmountPaid, 
         DueDate = invoice.DueDate, 
         InvoiceNo = invoice.ID, 
         UserId = invoice.UserId, 
         InvoiceItems = new List<InvoiceItemViewModel> { 
          Description = items.DESCRIPTION, 
          Quantity = items.Quantity, 
          Rate = items.Rate, 
          Id = items.ID, 
          InvoiceId = items.InvoiceId 
         } 
        } 
      ).SingleOrDefault(); 
+0

のように動作しますが、 – Baahubali

関連する問題