Convert static params to Hashtable and use with lambda search

hashtable c#
hash table implementation in c#
c# hashtable get value by key
hashtable vs dictionary c#
generic hashtable c#
hashtable in c
when to use hashtable in c#
c# hashtable duplicate keys

This question is not a duplicate. I am looking for an approach in which I can pass in a Hashtable for any number of key-value params and related where clauses, and that hashtable can be dynamically integrated into the lambda search. The other post doesn't answer this.

I have 3 overload methods:

public static bool DoesRecordExist(string keyColumn, string keyValue, DataTable dt) {
 if (dt != null && dt.Rows.Count > 0) {
  bool exists = dt.AsEnumerable().Where(r => string.Equals(SafeTrim(r[keyColumn]), keyValue, StringComparison.CurrentCultureIgnoreCase)).Any();
  return exists;
 } else {
  return false;
 }
}

public static bool DoesRecordExist(string keyColumn1, string keyColumn2, string keyValue1, string keyValue2, DataTable dt) {
 if (dt != null && dt.Rows.Count > 0) {
  bool exists = dt.AsEnumerable().Where(r => string.Equals(SafeTrim(r[keyColumn1]), keyValue1, StringComparison.CurrentCultureIgnoreCase) && string.Equals(SafeTrim(r[keyColumn2]), keyValue2, StringComparison.CurrentCultureIgnoreCase)).Any();
  return exists;
 } else {
  return false;
 }
}

public static bool DoesRecordExist(string keyColumn1, string keyColumn2, string keyColumn3, string keyValue1, string keyValue2, string keyValue3, DataTable dt) {
 if (dt != null && dt.Rows.Count > 0) {
  bool exists = dt.AsEnumerable().Where(r => string.Equals(SafeTrim(r[keyColumn1]), keyValue1, StringComparison.CurrentCultureIgnoreCase) && string.Equals(SafeTrim(r[keyColumn2]), keyValue2, StringComparison.CurrentCultureIgnoreCase) && string.Equals(SafeTrim(r[keyColumn3]), keyValue3, StringComparison.CurrentCultureIgnoreCase)).Any();
  return exists;
 } else {
  return false;
 }
}

These all work as expected, and as you can see, all 3 are identical, just with increasing numbers of params and corresponding where clause arguments. And now I need to add yet another overload with 5 key/value pairs. Clearly this is getting silly.

How can I convert all these overloads into a single function in which I simply pass in a Hashtable of key-value pairs (or some other collection that makes equal or better sense)?

Thanks in advance.

This problem is much simpler if you break it into parts.

First write a function that checks a single record for a match:

private static bool IsMatch(DataRow row, Dictionary<string,object> filters)
{
    return filters.All( pair => row[SafeTrim(pair.Key)].Equals(pair.Value) );
}

Then pass this as the delegate in your DoesRecordExist logic:

public static bool DoesRecordExist(Dictionary<string,object> filters, DataTable dt)
{
    if (dt == null || dt.Rows.Count == 0) return false;
    return dt.AsEnumerable().Any(r => IsMatch(r, filters));
}

The above is all you need.

Here is an example use:

public static DataTable CreateTestData()
{
    var data = new []
    {
        new { ID = 1, Name = "John",  DOB = new DateTime(2018,1,1) },
        new { ID = 2, Name = "Paul",  DOB = new DateTime(2018,1,2) },
        new { ID = 3, Name = "Ringo", DOB = new DateTime(2018,1,3) },
        new { ID = 4, Name = "George",DOB = new DateTime(2018,1,4) }
    };
    var table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("DOB", typeof(DateTime));
    foreach (var d in data)
    {
        var row = table.NewRow();
        row[0] = d.ID;
        row[1] = d.Name;
        row[2] = d.DOB;
        table.Rows.Add(row);
    }
    return table;
}

public static void Main()
{
    var table = CreateTestData();

    var filter1 = new Dictionary<string,object> { {"ID", 1 } };
    Console.WriteLine("Filter1 exists? {0}", DoesRecordExist(filter1, table));  //Should be true

    var filter2 = new Dictionary<string,object> { { "ID", 1 }, {"Name", "John" } };
    Console.WriteLine("Filter2 exists? {0}", DoesRecordExist(filter2, table));  //Should be true

    var filter3 = new Dictionary<string,object> { { "ID", 1 }, {"Name", "John" }, {"DOB", new DateTime(2018,1,31)} };
    Console.WriteLine("Filter3 exists? {0}", DoesRecordExist(filter3, table));  //Should be false

    var filter4 = new Dictionary<string,object> { { "ID", 1 }, {"Name", "Paul" }, {"DOB", new DateTime(2018,1,2)} };
    Console.WriteLine("Filter4 exists? {0}", DoesRecordExist(filter4, table));  //Should be false
}

Output:

Filter1 exists? True
Filter2 exists? True
Filter3 exists? False
Filter4 exists? False

DotNetFiddle

Fundamentals of Computer Programming with C#: The Bulgarian C# Book, For the conversion you can use the static method Convert.ToDecimal(). and LastName. 5. For the LINQ query use from, orderby, descending and select. As you can see that lambda expression has access to the this and super. Calling this.toString() from the lambda prints an instance of LambdaExample not of TestInterface. That’s all for the topic Variable Scope in Java Lambda Expression. If something is missing or you have something to share about the topic please write a comment.

By separating the concerns a little you can see a composite design pattern will solve this issue. Your method is basically...

public static bool DoesRecordExist(IPredicate<DataRow> condition, DataTable dt) {
    if (dt != null && dt.Rows.Count > 0) {
        bool exists = dt.AsEnumerable().Any(r => predicate.Condition(r));
        return exists;
    } else {
        return false;
    }
}

Where IPredicate<T> is an interface with one method: bool Condition(T t).

Now you can define a implementation of that interface for a DataRow, to represent your column/value matching for a single column:

public class DataRowPredicate
    : IPredicate<DataRow>
{
    private readonly string _keyColumn;
    private readonly string _keyValue;

    public DataRowPredicate(string keyColumn, string keyValue)
    {
        _keyColumn=keyColumn;
        _keyValue=keyValue;
    }

    public bool Condition(DataRow r)
    {
        return string.Equals(SafeTrim(r[_keyColumn]), _keyValue, StringComparison.CurrentCultureIgnoreCase); 
    }
}

Now all you need is the ability to stack those conditions together - you need the composite pattern: a collection which implements the same interface as the thing it contains. Something like...

public class PredicateCollection<T> : List<IPredicate<T>>, IPredicate<T>
{
    public bool Condition(T t)
    {
        return this.All(x => x.Condition(t));
    }
}

So now you can stack up as many as you like by creating a new collection and adding as many predicates as you require to it. Then you pass that collection as the parameter to DoesRecordExist. If you need to reuse the same collection of predicates a lot, just keep the collection around. Something like...

var conditionA=new PredicateCollection<DataRow>
{
    new DataRowPredicate(keyColumn1, keyValue1),
    new DataRowPredicate(keyColumn2, keyValue2),
    new DataRowPredicate(keyColumn3, keyValue3),
    //... etc, as required
};

And use it like this...

bool result = DoesRecordExist(conditionA, dt);   

Disclaimer: I'm not at my PC, so cut me some slack if this doesn't compile, or contains any autocorrections!

C# Hashtable with Examples, Convert static params to Hashtable and use with lambda search. This question is not a duplicate. I am looking for an approach in which I can pass in a  C# program that uses lambda, Func using System; class Program { static void Main() {// Part 1: use implicitly-typed lambda expression. Assign it to a Func instance.

You can pass in an enumerable of your search items and loop over them. For each item just chain an extra call to Where onto your result.

I've created some sample code to hopefully illustrate what I mean. I've not fully tested it but hopefully if it doesn't work as is it should give you the right idea. It basically works on the principle that .Where(A&B) is basically the same as .Where(A).Where(B)

public static bool DoesRecordExist(DataTable dt, Dictionary<string,string> searchDetails) 
{
    if (dt != null && dt.Rows.Count > 0) {
        var items = dt.AsEnumerable();
        foreach(var searchItem in searchDetails)
        {
            items = items.Where(r=>string.Equals(SafeTrim(r[searchItem.Key]), searchItem.Value, StringComparison.CurrentCultureIgnoreCase)
        }
        return items.Any();
    } 
    else 
    {
        return false;
    }
}

Language Integrated Query (LINQ) and Lambda Expressions, Or in other words, a Hashtable is used to create a collection which uses a It generally optimized the lookup by calculating the hash code of every key in DictionaryEntry, so you can also cast the key/value pairs to a DictionaryEntry. static public void Main() WriteAllText(String, String) Method in C# with Examples · File. A Python lambda function behaves like a normal function in regard to arguments. Therefore, a lambda parameter can be initialized with a default value: the parameter n takes the outer n as a default value. The Python lambda function could have been written as lambda x=n: print(x) and have the same result.

something like this would help

public static bool DoesRecordExist(DataTable dt, params string[] parameters)
{
    bool exists = false;

    if (parameters.Length % 2 != 0 )
        throw new ArgumentException();

    if (dt == null || dt.Rows.Count == 0)
        return false;

    var query = dt.AsEnumerable()

    for (int i = 0; i < parameters.Length; i += 2)
        query = query.Where(r => string.Equals(SafeTrim(r[parameters[i]]),
            parameters[i + 1], StringComparison.CurrentCultureIgnoreCase)) ; 

    return exists;

}

C# Hashtable with Examples, public static void ExecuteCSharp6_0() { // declare the lambda expression Use the EnumerableMessageQueue class to write a LINQ query to retrieve To find all Employee s on all projects, we can use Union to get all Once we have that tree, LINQ to SQL then has to convert it to actual SQL that can run ArrayList type. The following example demonstrates how to use a lambda expression in a method-based query by using the Enumerable.Where standard query operator. Note that the Where method in this example has an input parameter of the delegate type Func<T,TResult> and that delegate takes an integer as input and returns a Boolean.

C# Hashtable, A hash table is a special collection that is used to store key-value items. Linq; using System.Text Tasks; namespace DemoApplication { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht. We use the lambda expression syntax with Func type instances to store anonymous methods. We see type parameters: these are specified in the angle brackets < and >.Lambda. So: The first type parameters are the arguments to the methods, and the final type parameter is the return value. Generic Class, Method Return

C# ToDictionary Method, NET MVC · IoC · web api · C# · LINQ The Add() method adds an item with a key and value into the Hashtable. Key and Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(1 So you cast each element in Hashtable to DictionaryEntry. Use the foreach statement to iterate the Hashtable, as shown below:. Python map object is also iterable holding the list of each iteration. We can also convert it to List or Dictionary or other types using their constructor functions. In this tutorial, you’ll learn how to use the map() function with different types of sequences. Also, you can refer to the examples that we’ve added to bring clarity.

Programming Language Pragmatics, This extension method converts a collection into a Dictionary. Linq; class Program { static void Main() { // Example integer array. int[] values = new int[] { 1, 3​, 5,  The above lambda expression has 1 parameter, so this collection contains one node: § A ParameterExpression instance, representing the int parameter named “int32”. o A Body node representing the lambda expression’s body, which is a BinaryExpression instance, representing the body is a “>” (greater than) comparison operation of 2 operands.

10 Examples of Converting a List to Map in Java 8, forms, 499-503 separate compilation, 534 stack layout, 434 wide use of, 499 GCD, Scr See Free Software Foundation; goal in Prolog, 625 goat -directed search, 290 Gde1 in lambda calculus, 618-619 logic programming, 592 machine language. 162, 489 hash function, 135, 139,517 hash table for associative array. In this article, we’re going to look at new and helpful tools in NetBeans IDE 8 that enable developers to use the functional features of Java SE 8. Java SE 8 introduces lambda expressions to the language. Figure 1 shows a lambda expression in Java. The parameters are shown on the left side of the lambda arrow, while the method body is shown to its right.

Comments
  • tip: instead of taking N arguments matching N column names, pass a collectio of strings as argument...
  • What have you tried? It seems it should be as easy as looping through your pairs adding an extra Where method call in for each pair - assuming you realise that .Where(A&B) is basically the same as .Where(A).Where(B)...
  • Thanks Jota and Chris. I'm struggling with how a loop would even be constructed inside a lambda expression. Not sure I have ever seen that. Can you give an example?
  • @HerrimanCoder: the loop isn't inside the lambda. I've added an answer with some code that hopefully illustrates what I mean.
  • After I fixed my bug, this works well! Thanks everyone.
  • Example usage please? :-)
  • .All() requires arguments - any ideas?
  • With my data, this function returns false when it should return true. Maybe I'm doing something wrong, not sure yet.
  • @HerrimanCoder Sorry, I had the return statements backwards: if the string matches we want to return true out of the inner loop. We only want to return false when nothing matches (i.e. we get to the end of the inner loop without finding any matches)