How to select all fields plus some new fields in LINQ?

Go To StackoverFlow.com

5

  1. I got an RDLC report
  2. Date Format stored in SQL DB is Georgian. I want to Display date in report as Persian.
  3. Using Linq i want to select all fields of DB in addition some new field to be used as Persian Date Fields.

I used below syntax:

    var invoices = from invoice in dbContext.eve_Invoices
                    select new
                    {

                        invoice.CreatorID,
                        invoice.DateChange,
                        invoice.DateCreation,
                        invoice.DatePrint,
                        invoice.Discount,
                        invoice.DiscountPercentage,
                        invoice.DiscountType,
                        invoice.Fare,
                        invoice.ID,
                        invoice.InvoiceStatus,
                        invoice.NumberOfItems,
                        invoice.Packaging,
                        invoice.PrintID,
                        invoice.RawPrice,
                        invoice.RoundOff,
                        invoice.ServiceCharge,
                        invoice.ServingType,
                        invoice.TableID,
                        invoice.Tax,
                        invoice.TotalPrice,
                        invoice.ValidityStatus,
                        PersianYear = invoice.DateCreation != null ? pc.GetYear((DateTime)invoice.DateCreation) : 0,
                        PersianMonth = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0,
                        PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0
                    };

is there any way to pass all fields of invoice in addition to new fields such as:

    var invoices = from invoice in dbContext.eve_Invoices
                    select new
                    {

                        invoice.*,
                        PersianYear = invoice.DateCreation != null ? pc.GetYear((DateTime)invoice.DateCreation) : 0,
                        PersianMonth = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0,
                        PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0
                    };

I also tried select new {invoice,....} however it will not result to datatable same as first one.

2012-04-05 21:40
by VSB
Your first syntax wont work, you should have named parameter for your anonymous type, Also I think DateTime casting wont work here - Saeed Amiri 2012-04-05 22:35


5

You can pass all the feilds, but it's not beautiful, it's better to pass invoice itself, instead of its fields:

var invoices = from invoice in dbContext.eve_Invoices
                select new
                {
                    RelatedInvoice = invoice,
                    PersianYear = invoice.DateCreation != null ? pc.GetYear((DateTime)invoice.DateCreation) : 0,
                    PersianMonth = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0,
                    PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0
                };

P.S: I'm not sure this lines work carefully in linq2entities(it is some days i don't have any IDE and I forgot everything:):

PersianDay = invoice.DateCreation != null ? pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0

If this doesn't work I suggest first fetch data and then do conversion.

2012-04-05 22:33
by Saeed Amiri
thanks for your help. The main problem is i want to result of this query to inject data into an RDLC report and i don't know how to convert Invoice Data Type to basic data types which can be used as basic data types in RDLC report. As far as i know, RDLC does not support data types such as invoice : - VSB 2012-04-06 14:02
There is no way in linq2entities, except using something similar to your first sample, but there is a dirty way in linq2object by reflection, but I'd like normal way, first I'll fetch data from DB, then I'll convert them in e.g UI format - Saeed Amiri 2012-04-06 14:06


0

You can use the invoice itself.

var invoices = from invoice in dbContext.eve_Invoices
               select new
               {
                 invoice,
                 PersianYear = invoice.DateCreation != null ? >pc.GetYear((DateTime)invoice.DateCreation) : 0,
                 PersianMonth = invoice.DateCreation != null ? >pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0,
                 PersianDay = invoice.DateCreation != null ? >pc.GetDayOfMonth((DateTime)invoice.DateCreation) : 0
               };

You can later on access the fields like this

 foreach (var invc in invoices)
  { 
     //invc.invoice.CreatorID
  }

The date time casting also works. You should take care if invoice.DateCreation is Nullable type before casting it.

2012-04-06 07:27
by userGS
thanks for your help. The main problem is i want to result of this query to inject data into an RDLC report and i don't know how to convert Invoice Data Type to basic data types which can be used as basic data types in RDLC report. As far as i know, RDLC does not support data types such as invoice : - VSB 2012-04-06 14:01
Ads