0

Where clause and Select clause can be dynamic.

I have the below code, which is working well for static columns

var q = (from a in Parse_CSV(@"fileName1").AsEnumerable()
                     join b in Parse_CSV(@"fileName2").AsEnumerable()
                       on a.Field<string>(0) equals b.Field<string>(0)
                     where a.Field<string>(1) != b.Field<string>(1) ||
                           a.Field<string>(2) != b.Field<string>(2)
                     select new
                     {                            
                         col1 = a.Field<string>(0),
                         col2 = a.Field<string>(1) == b.Field<string>(1) ?
                                        "0" : a.Field<string>(1),
                         col3 = a.Field<string>(2) == b.Field<string>(2) ?
                                        "0" : a.Field<string>(2)
                     }).ToList();

how can I change,

  1. In where column, a.Field(x) -> x from 1 to length. In

  2. select column, col1 = a.Field(0) -> col1 till... length

Where length is known number at run time

Rahul
  • 53
  • 1
  • 8
  • Let's say you managed to get it working, how exactly would you consume `q`? – DavidG Jul 05 '19 at 11:58
  • Hi David, I have code to change it to csv format – Rahul Jul 05 '19 at 12:00
  • is it similar to this question? https://stackoverflow.com/questions/9505189/dynamically-generate-linq-queries – Kiran Paul Jul 05 '19 at 12:00
  • @Rahul Yes, but how would your code know if the `colXX` property exists? – DavidG Jul 05 '19 at 12:02
  • Note that adding another `colXX` property makes the anonymous object a *completely different type*. – DavidG Jul 05 '19 at 12:03
  • Hi Kiran, Thanks - but unfortunately it's not the same. The suggested question passing parameter value dynamically(same number of parameters). But in this case parameters numbers can be dynamic. – Rahul Jul 05 '19 at 12:03
  • Hi David, I understand - result I am able to get it with static columns csv. I am unable to paste code for the results as it's more than 400 characters – Rahul Jul 05 '19 at 12:05
  • Can you rephrase your 2 questions. For the 1st question: `a.Field(1)` needs to be replaced by what again? Same for your 2nd question, `col1 = a.Field(0)` needs to be replaced by what again? – Vikhram Jul 05 '19 at 12:32
  • Hi Vikhram - as I mentioned, both questions should be replaced from 1 to length, length is known number at run time – Rahul Jul 05 '19 at 12:58

0 Answers0