0

I am getting a list of object with type List and I want that records form the list which EventType = "Changed" EventDesc = "Changed" and UserName = "zMobile" But when i try to get the result with my code i am getting only one row multiple times with the same data

orderEvent = new List<ECGetOrderResponse.OrderEvent>();
EventType = "Changed";
EventDesc = "Changed";
UserName = "zMobile";
orderEvent = result.Body.Order.OrderEvents.OrderEvent;
query = orderEvent
   .Where(o => o.EventType == EventType)
   .Where(o => o.EventDesc == EventDesc)
   .Where(o=>o.UserName==UserName)
   .ToList();

One More thing i want to do in the linq code that is: the orderEvent has a property that is Notes. i want to filter only that record in which Notes Last 1 value is 'A'

can you describe what will be the correct format of the Linq statement for my result?

A.Goutam
  • 3,422
  • 9
  • 42
  • 90
  • 1
    What do you want your code to do? What does it **actually** do? Can you show us some sample inputs and matching sample outputs? – mjwills Aug 21 '17 at 11:39
  • Why you initialize the list `orderEvent` twice? That's just confusing and would never avoid a `NullReferenceException`(if that was the reason). – Tim Schmelter Aug 21 '17 at 11:40
  • `orderEvent .Where(o => o.EventType == EventType && o.EventDesc == EventDesc && o.UserName==UserName && o.Notes.EndsWith("A") .ToList();` – Yogen Darji Aug 21 '17 at 11:42
  • 1
    Add `.Where(o => o.Notes.EndsWith( "A" ))` – Dmitry Aug 21 '17 at 11:45

1 Answers1

3

when i try to get the result with my code i am getting only one row multiple times with the same data

With this code it's impossible to get always the same row, so result.Body.Order.OrderEvents.OrderEvent already contained these rows before. But you haven't shown what it is so we can't help to fix it.

i want the orderEvent has a property that is Notes. In the notes field last 1 character is 'A'

Well, that is easy:

query = orderEvent
   .Where(o => o.EventType == EventType)
   .Where(o => o.EventDesc == EventDesc)
   .Where(o => o.UserName  == UserName)
   .Where(o => o.Notes.EndsWith("A", StringComparison.CurrentCultureIgnoreCase)) 
   .ToList();

Change StringComparison accordingly if you don't want to allow a.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I would suggest including all conditions in one where clause, for performance reason. – Dmitry Aug 21 '17 at 11:52
  • @Dmitry: there is no noticeable difference between both approaches so if OP prefers this way i don't want to suggest a different. But i wondered myself some time ago, read [here](https://stackoverflow.com/q/23674778/284240). – Tim Schmelter Aug 21 '17 at 11:53
  • @TimSchmelter, there might be a noticeable performance difference. Can't tell without testing. – Jodrell Aug 21 '17 at 11:57
  • @Jodrell: well, i have posted the link to my question, you can find a test there – Tim Schmelter Aug 21 '17 at 11:59