0

I am wondering if there is away to query a db set by the use of a variable in linq.

First of all I query my database to get the name of the table as a string, I want to convert this to a table name:

    public static string getTableName()
    {
        string tableName = string.Empty;
        var department= "Sales";

        using (var context = new ALLDBEntities())
        {
            tableName = (from x in context.PROCESS_MATRIX
                where x.AREA.Equals(product)
                select x.ENTITY).FirstOrDefault();
        }

        return tableName;
    }

Now I have my table name as a string, I want to be able to use this to write a linq query, but instead of using alldbEntities.sales, I want to use table. The reason for this is because I will need to write multiple queries to do the same thing, as I will have to query different tables.

Is there away to achieve this?

    public List<sales> GetData(DateTime startDate, DateTime endDate)
    {
        var table = getTableName();

        this.startDate = startDate.AddDays(-1);
        this.endDate = endDate.AddDays(1);

        using (var alldbEntities = new ALLDBEntities())
        {
            salesinfo = alldbEntities.sales.Where(f => f.Date >= this.startDate && f.Date <= this.endDate).ToList();
        }

        return salesinfo;
    }
4
  • i want to be able to use this to write a linq query? LInq refers only to list and tables not literally the table name of your Database itself. Commented Apr 25, 2018 at 10:04
  • Just because you want something to be variable doesn't necessarily mean that you need to use a string to do so. Are you open to solutions that make the source table variable, without using strings? Commented Apr 25, 2018 at 10:04
  • @flater yeah i am opened to other solutions, this is the way i envisage it, but it obvs doesnt work, Commented Apr 25, 2018 at 10:05
  • Possible duplicate of Querying data using Entity Framework from dynamically created table Commented Apr 25, 2018 at 11:36

1 Answer 1

3

Here your are: Scott shows you how to use Dynamic LINQ

Things you could do:

The Dynamic Expression API is brought into scope by using (importing) the System.Linq.Dynamic namespace. Below is an example of applying the Dynamic Expression API to a LINQ to SQL data source.

var query =
    db.Customers.
    Where("City = @0 and Orders.Count >= @1", "London", 10).
    OrderBy("CompanyName").
    Select("new(CompanyName as Name, Phone)");

Remember to try innerIt and outerIt

An example of how to use outerIt

static void Main(string[] args)
{
    var claims = new List<Claim>();
    claims.Add(new Claim { Balance = 100, Tags = new List<string> { "Blah", "Blah Blah" } });
    claims.Add(new Claim { Balance = 500, Tags = new List<string> { "Dummy Tag", "Dummy tag 1" } });

    // tags to be searched for
    var tags = new List<string> { "New", "Blah" };
    var parameters = new List<object>();
    parameters.Add(tags);

    var query = claims.AsQueryable().Where("Tags.Any(@0.Contains(outerIt)) AND Balance > 100", parameters.ToArray());
}

public class Claim
{
    public decimal? Balance { get; set; }
    public List<string> Tags { get; set; }
}

Generate Dynamic Linq query using outerIt

Sign up to request clarification or add additional context in comments.

2 Comments

Answers consisting of nothing but an external link are not good answers. Links can deprecate. Add the relevant information to your answer directly to avoid quality issues in the future.
I just updated my answer. Thanks for your replay though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.