How To Show Error Message In Linq C# [Resolved]

Posted by Kasani007 under LINQ on 12/3/2015 | Points: 10 | Views : 575 | Status : [Member] | Replies : 1
How To Show Error Message In Linq C#

Below Is Code for my requirement
I wanna display error message if received amount is greater than total amount using LinQ query



var invoiceInfo = from invoice in InvoiceData
if(invoice.ReceivedAmount>invoice.TotalAmount)
MessageBox.Show("You Have Entered More Amount Than Invoice Amount")
else
select new InvoiceMaster
{
InvoiceId = invoice.InvoiceId,
InvoiceNumber = invoice.InvoiceNumber,
InvoiceDate = invoice.InvoiceDate,
TotalAmount = invoice.TotalAmount,
ReceivedAmount = invoice.ReceivedAmount

}




Responses

Posted by: Rajnilari2015 on: 12/3/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
You can use Statement Lambda for this purpose. An workable solution is presented below

using System;

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var invoiceDataList = new List<InvoiceData>();

//populating some data
Enumerable
.Range(1, 10)
.ToList()
.ForEach(
i =>
invoiceDataList.Add(new InvoiceData
{
InvoiceId = i,
InvoiceNumber = i + 10,
InvoiceDate = DateTime.Now.AddDays(i),
TotalAmount = i % 2 == 0 ? 20000 + i : 1000 + i,
ReceivedAmount = i % 2 == 0 ? 1000 + i: 20000 + i
})
);

//the program
var InvoiceMasterList = new List<InvoiceMaster>();
invoiceDataList
.ToList()
.ForEach(i =>
{
if (i.ReceivedAmount > i.TotalAmount)
Console.WriteLine("You Have Entered More Amount Than Invoice Amount");
else
InvoiceMasterList.Add(new InvoiceMaster
{
InvoiceId = i.InvoiceId,
InvoiceNumber = i.InvoiceNumber,
InvoiceDate = i.InvoiceDate,
TotalAmount = i.TotalAmount,
ReceivedAmount = i.ReceivedAmount
});
});


}
}

public class InvoiceData
{
public int InvoiceId { get; set; }
public int InvoiceNumber { get; set; }
public DateTime InvoiceDate { get; set; }
public int TotalAmount { get; set; }
public int ReceivedAmount { get; set; }

}

public class InvoiceMaster
{
public int InvoiceId { get; set; }
public int InvoiceNumber { get; set; }
public DateTime InvoiceDate { get; set; }
public int TotalAmount { get; set; }
public int ReceivedAmount { get; set; }

}
}

Hope this helps.


--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response