Home > Mobile >  How to put contains on DataTable Rows using LINQ
How to put contains on DataTable Rows using LINQ

Time:02-01

This works:

 exists = dt.AsEnumerable().Where(c => c.Field<int>("GENARTNR").Equals(int.Parse(nodeID))).Count() > 0;

Now nodeID is a string which can have more than 1 nodeIdz so 100,178,111,200 or 100 or 200,100

Now I want to do something like:

  exists = dt.AsEnumerable().Where(nodeID.Contains(c => c.Field<string>("GENARTNR")));  

But I am getting :

Cannot convert lambda expression to type 'char' because it is not a delegate type

Any work around this or some other suggestion?

CodePudding user response:

First, don't use Field<string>("GENARTNR") is it's actually an int column.

You can use this approach that uses Contains with an int[]:

int[] nodeIDs = ParseNodeId(nodeId);
exists = dt.AsEnumerable().Any(r => nodeIDs.Contains(r.Field<int>("GENARTNR")));  

private static int[] ParseNodeId(string nodeId)
{
    return nodeId?.Trim().Split(',')
        .Select(n => int.TryParse(n.Trim(), out int id) ? id : (int?)null)
        .Where(x => x.HasValue)
        .Select(x => x.Value)
        .ToArray() ?? Array.Empty<int>();
}
  • Related