Object to IQueryable

EDIT (2022): julealgon prompted me to look back on this code after many years, and I have to agree with them that the array initialization isn't necessary. I quickly came up with the code below which can be used as a baseline to implement this yourself.

void Main()
{
    var singleValueQueryable = "Foo".ToQueryable();

    foreach (var item in singleValueQueryable)
    {
        Console.WriteLine(item);
    }
}

public static class ObjectExtensionMethods
{
    public static IQueryable<T> ToQueryable<T>(this T instance)
    {
        return new SingleValueQueryable<T>(instance);
    }
}

public class SingleValueQueryable<T> : IQueryable<T>, IEnumerator<T>
{
    private bool hasExecutedOnce = false;

    public SingleValueQueryable(T value)
    {
        this.Current = value;
    }

    public Type ElementType => typeof(T);

    public Expression Expression => throw new NotImplementedException();

    public IQueryProvider Provider => throw new NotImplementedException();

    public IEnumerator<T> GetEnumerator()
    {
        return this;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    public T Current { get; set; }

    object IEnumerator.Current => this.Current;

    public void Dispose()
    {
    }

    public bool MoveNext()
    {
        if (hasExecutedOnce)
        {
            return false;
        }

        hasExecutedOnce = true;
        return true;
    }

    public void Reset()
    {
        this.hasExecutedOnce = false;
    }
}

Below is a benchmark of the three methods. QueryableMethod is the method shown above. ArrayMethod is the method shown below the benchmark (which was the previous answer). YieldMethod is simply a yield return in combination with AsQueryable().

Method Mean Error StdDev Gen 0 Allocated
QueryableMethod 7.547 ns 0.1693 ns 0.1811 ns 0.0019 32 B
ArrayMethod 26.786 ns 0.2870 ns 0.2684 ns 0.0072 120 B
YieldMethod 30.609 ns 0.3935 ns 0.3680 ns 0.0062 104 B

EDIT (2017): like kienct89 I created a generic extension method for easier use:

public static class ObjectExtensionMethods
{
    public static IQueryable<T> ToQueryable<T>(this T instance)
    {
        return new [] {instance}.AsQueryable();
    }
}

The method can be used simply calling it on the object you created:

object anObject = new object();
anObject.ToQueryable();

@kienct89: Wow that is almost exactly what I did, even the names match, I just took array because it seemed less expensive, you did it in one line:

public static class ObjectExtensionMethods
{
    public static IQueryable<T> ToQueryable<T>(this T instance)
    {
        TEntityType[] arrayOfObject = {instance};
        return arrayOfObject.AsQueryable();
    }
}

With both our answers I changed it to:

public static class ObjectExtensionMethods
{
    public static IQueryable<T> ToQueryable<T>(this T instance)
    {
        return new [] {instance}.AsQueryable();
    }
}

I don't know why you need to return IQueryable, but I think it's better to create an extension class to convert object to IQueryable. That would help to achieve 3 things

  1. Easier to update the code in future since you only have to manage the conversion logic in 1 file
  2. Less duplicated codes ==> less bugs
  3. Easier to test

Assuming you have 3 model classes

public class Entity
{
    public int Id {get; set;}
}

public class Customer : Entity
{
    public string Name {get; set;}
}

public class Cart : Entity 
{
}

We can write extension class like this

public static class QueryableExtension
{
    public static IQueryable<T> ToQueryable<T>(this T obj) where T : Entity
    {
        return new List<T> { obj }.AsQueryable();
    }
}

How to use

var customer = new Customer { Id = 1, Name = "test" };
var cart = new Cart { Id = 1 };
IQueryable<Customer> customers = customer.ToQueryable();
IQueryable<Cart> carts = cart.ToQueryable();