I have a very specific LINQ query. I would like to check the existence of a randomly generated key in a table.
The standard query could be defined as Select * from Products where SaleId == 'XXXXXXX'.
In this query the XXXXXX is generated by a random character generator (a length is also provided). I created the following LINQ extension:
public static string GetUniqueId<T, TProperty>(this IEnumerable<T> source, int length, Func<T, TProperty> idProperty)
{
bool isUnique = false;
string uniqueId = String.Empty;
while (!isUnique)
{
uniqueId = PasswordGenerator.GenerateNoSpecialCharacters(length);
if (!String.IsNullOrEmpty(uniqueId))
{
isUnique = source.AsQueryable().SingleOrDefault(i => idProperty(i).Equals(uniqueId)) == null;
}
}
return uniqueId;
}
However, I have noticed that this method first selects all the records from the table that is passed as a source and then runs the Where clause. This behavior is obviously very time consuming. So basically it does SELECT * FROM Products and then runs the SingleOrDefault
Is there any way I could directly run the query such that it does Select * from Products WHERE Id = ‘XXXXXXX’
Here’s an example of how I call it:
string id = c.L2SOnlineCountMasters.GetUniqueId(9, x => x.MID);
In this case L2SOnlineCountMasters is a table in the databse and c is the DataContext instance.
After reading both the comments, I realized that IQueryable should be used.
However, “Equals” in Expression Call doesn’t work as it throws the following error:
“More than one method ‘Equals’ on type ‘System.String’ is compatible with the supplied arguments.” Thus I modified the code a little bit as follows:
That really solved the issue.