Click here to Skip to main content
16,019,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using linq expression in c#, but it trigger error of specified case is not valid, can any body show me the better way of my code, thanks

What I have tried:

var newDt = dt.AsEnumerable()
               .GroupBy(r => new { name = r["name"], email = r["email"], phoneNo=
                r["phone_no"], checkIn = r["check_in"], checkOut =
                r["check_out"],noDate = r["no_date"], clientValidationCode =
                r["client_validation_code"],clientBookingType =
                r["client_booking_type"],clientPaymentGateway =
                r["client_payment_gateway"], clientCreatedAt =
                r["client_created_at"],status = r["status"]
                   })
                   .Select(g => new
                   {
                       Name = g.Key.name,
                       Email= g.Key.email,
                       PhoneNo= g.Key.phoneNo,
                       CheckIn= g.Key.checkIn,
                       CheckOut= g.Key.checkOut,
                       NoDate= g.Key.noDate,
                       ClientValidationCode= g.Key.clientValidationCode,
                       ClientBookingType= g.Key.clientBookingType,
                       ClientPaymenetGateway= g.Key.clientPaymentGateway,
                       ClientCreatedAt= g.Key.clientCreatedAt,
                       Status =g.Key.status,
                       type_no = string.Join(",", g.Select(z => z.Field<string>
                       ("room_no"))),
                       clientBookingId = g.Min(r=> r.Field<int>("client_booking_id")),
                       extraDiscount = g.Sum(r => r.Field<decimal>("extra_discount")),
                       netPrice = g.Sum(r => r.Field<decimal>("netPrice")),
                       commission = g.Sum(r => r.Field<decimal>("commission")),
                       vatOrg = g.Sum(r => r.Field<decimal>("vat_org")),
                       vatGetrest = g.Sum(r => r.Field<decimal>("vat_getrest")),
                       payed = g.Sum(r => r.Field<decimal>("payed")),
                       unPayed = g.Sum(r => r.Field<decimal>("un_payed")),

                   });
Posted
Updated 27-Sep-21 20:19pm

1 solution

One (or more) of the fields in your DataTable contains data that can't be interpreted in eth form you want: it cannot be cast to an integer for example because it is a string containing "Hello".

We can't tell you which row, and we can't tell you which column - because we have no access to your data or your code while it is running, and you need both to start solving this.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
You will probably have to manually check your DataTable to find the error (or write code to loop though each row and do what the Linq code is doing, breaking the linq code into "chunks" so you can examine the actual data) to identify the row and then column that causes the problem.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900