3

I had tried a similar SO answer here which not worked and might be missing something in my case.

Background:

I'm trying to pull a list of Trade Instruments from an external API ( around 8k records ) from which I need around 10 only. So trying to filter it as below , but the filter results are 0 .

Model:

public class Trade
{
    public int ID { get; set; }
    public string Scrip { get; set; }
    public int Quantity { get; set; }            
}

Filtering:

List<Trade> trades;
using (StreamReader sr = new StreamReader(Server.MapPath("~/Utils/trades.json")))
{
    trades = JsonConvert.DeserializeObject<List<Trade>>(sr.ReadToEnd());
}

List<Instrument> instruments = GetInstruments(Exchange: "NY");// count 8k
var result = instruments.Where(x => trades.Any(n => x.Name.Contains(n.Scrip))); //count 0

Also, tried to fetch the Scrip names from the trades list as string array and used for filtering which also didn't work.

Please advise and thanks in advance.

15
  • 2
    what does the debugger say? nail it down on one occurrence of trades.Any(n => x.Name.Contains(n.Scrip)), prove that it will be true for at least one entry in the actual data. Is it something basic, like a case mismatch (string comparisons are case sensitive by default)? Commented Jan 9, 2018 at 8:33
  • @dlatikay debugger not giving me much hints on this Commented Jan 9, 2018 at 8:35
  • 1
    make it! you're its boss :) Commented Jan 9, 2018 at 8:36
  • Are you certain that there is an instrument whose Name contains one the Script text of one of the items in trades? Commented Jan 9, 2018 at 8:38
  • 2
    And you sure the casing is exactly the same as well? Maybe you should try using a standard foreach loop instead of linq so you can set a conditional breakpoint inside the loop. The code looks ok, but without actual sample data there's only so much we can do. Commented Jan 9, 2018 at 8:45

1 Answer 1

1

Thanks guys for the help , was a silly mistake

changed Contains to Equals and its working as expected.

instruments.Where(x => trades.Any(n => x.Name.Equals(n.Scrip)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.