question

GaniTPT avatar image
0 Votes"
GaniTPT asked GaniTPT commented

How do we find of two column values in another column using LINQ C#

I have 3 columns and it contains some values.

I want to find of two column (COL1 and COL2) values in another column (COL3)

i.e. COL1 and COL2 values should check in COL3.

If any values found in in COL3, it should return true, else false.

      DataTable tblData = new DataTable();
      tblData.Columns.Add("COL1", typeof(string));
      tblData.Columns.Add("COL2", typeof(string));
      tblData.Columns.Add("COL3", typeof(string));
      tblData.Rows.Add("AM", "120", "AM-120");

Note : Here COL3 value may be either "AM-120" or "AM 120"

dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


How should it work in this case: tblData.Rows.Add("AM", "120", "SAM-1200")?

0 Votes 0 ·

Yes. this also true. because AM and 120 values exist in "SAM-1200" ==> returns true this case also...

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered GaniTPT commented

Try something like this:

 bool result = tblData.AsEnumerable( ).Any( r =>
  {
  string v1 = r.Field<string>( "COL1" );
  string v2 = r.Field<string>( "COL2" );
  string v3 = r.Field<string>( "COL3" );
    
  return v3.Contains( v1 ) || v3.Contains( v2 );
  } );

Use "&&" instead of "||" if you need both values. It is also possible to perform case-insensitive comparison.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. It's Fine.

0 Votes 0 ·