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;
    }
Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
user1483145
  • 129
  • 1
  • 2
  • 10
  • `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. – Vijunav Vastivch Apr 25 '18 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? – Flater Apr 25 '18 at 10:04
  • @flater yeah i am opened to other solutions, this is the way i envisage it, but it obvs doesnt work, – user1483145 Apr 25 '18 at 10:05
  • Possible duplicate of [Querying data using Entity Framework from dynamically created table](https://stackoverflow.com/questions/8896843/querying-data-using-entity-framework-from-dynamically-created-table) – Isma Apr 25 '18 at 11:36

1 Answers1

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

Isma
  • 14,604
  • 5
  • 37
  • 51
Jon Stan
  • 51
  • 5
  • 4
    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. – Flater Apr 25 '18 at 10:12
  • I just updated my answer. Thanks for your replay though – Jon Stan Apr 25 '18 at 10:52