Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3696828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:47:10+00:00 2026-05-19T04:47:10+00:00

I have a very specific LINQ query. I would like to check the existence

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:47:10+00:00Added an answer on May 19, 2026 at 4:47 am

    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:

    public static string GetUniqueId<T, TProperty>(this IQueryable<T> source, int length, Expression<Func<T, TProperty>> idProperty)
        {
            bool isUnique = false;
            string uniqueId = String.Empty;
            while (!isUnique)
            {
                uniqueId = PasswordGenerator.GenerateNoSpecialCharacters(length);
                if (!String.IsNullOrEmpty(uniqueId))
                {
                    var expr = Expression.Lambda<Func<T, bool>>(
                        Expression.Call(idProperty.Body, typeof(string).GetMethod("Equals", new[] { typeof(string) }), Expression.Constant(uniqueId)), idProperty.Parameters);
                    isUnique = source.SingleOrDefault(expr) == null;
                }
            }
    
            return uniqueId;
        }
    

    That really solved the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create very simple program that could help my relative to
I have a very specific html table construct that seems to reveal a Gecko
I have a very specific set of needs and am looking for a library
The problem that I have is somehow very specific. I have to implement a
I have a particular application that needs to calculate something very specific, and while
I have a custom closed-hashset/open-addressing (i.e. no linked lists) class. It's very specific to
I have a very odd issue. When I execute a specific database stored procedure
I am very much a beginner to rails and I have a specific need
this is a very specific question, but i also have very specific details on
I have a very specific problem, I want to search for the line containing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.