Elias
Elias

Reputation: 11

How to retrieve data from dynamic table in mvc

I'm writing new api call for my android app, I want to retrieve data from dynamic table, I mean when user select "A" button from Andriod I want to retrieve data from "A" table and if "B" is click , retrieve data from "B" table. Someone help me to find the possible solution.

I want to replace with variable for "JBCTNRITEMs" from entities.JBCTNRITEMs (table-name)

    var query = (from jbct in entities.JBCTNRITEMs
                 where jbid.Contains(jbct.jbid.ToString()) && boxid.Contains(jbct.TrackingNo.ToString())
                 select jbct).ToArray();
    int id = 0;
    if (query.Length > 0)
    {
        id = (query[0].id);
    }
    return id;

Here are the two different possibilities as an example

if (module.ToLower() == "module-a") 
{
      var imageinfo = (from jb in entities.TableA.AsEnumerable() 
                       where scanID.Contains(jb.aid.ToString()) 
                       select jb).ToArray(); 
       InsertGENIMAGE(userID, scanID, FilePath, imageinfo, "AIMAGE"); 
}
else if (module.ToLower() == "module-b") 
{
    var imageinfo = (from jb in entities.TableB.AsEnumerable() 
                     where scanID.Contains(jb.bid.ToString()) 
                     select jb).ToArray(); 
    InsertGENIMAGE(userID, scanID, FilePath, imageinfo, "BIMAGE"); 
}

Upvotes: 1

Views: 88

Answers (2)

Mong Zhu
Mong Zhu

Reputation: 23732

I guess in this case I would suggest to use a generic method :

private T GetMeTheFirstEntry<T>(Expression<Func<T, bool>> filter) where T : class
{
    return entities.GetTable<T>().FirstOrDefault(filter);
}

The GetTable will allow you to interchange the tableA and tableB. You would call it the following way:

TableA tableA_entity = GetMeTheFirstEntry<TableA>(jb => scanID.Contains(jb.aid.ToString()));
TableB tableB_entity = GetMeTheFirstEntry<TableB>(jb => scanID.Contains(jb.bid.ToString()));

If the filtering was successfull, then the retrieved object will not be null and you can use it:

int a_id = tableA_entity.aid;

Upvotes: 0

Jitendra Gupta
Jitendra Gupta

Reputation: 824

Here, query stores what you are you trying to select. As long as you are trying to select same type or same anonymous type, it will work.

Here is a simple example:

class Test1
{
     public int ID { get; set; }
     public string Name { get; set; }
}

class Test2
{
      public int ID { get; set; }
      public string Name { get; set; }
}

var test1Lst = new List<Test1>
{
      new Test1() { ID = 1, Name = "Jitendra" },
      new Test1() { ID = 2, Name = "Jahnavi" }
};

var test2Lst = new List<Test2>
{
      new Test2() { ID = 1, Name = "Aaditri" },
      new Test2() { ID = 2, Name = "Pankaj" }
};

var test = false;
var query = test ? (from t in test1Lst select new { ID = t.ID, Name = t.Name }) : (from t in test2Lst select new { ID = t.ID, Name = t.Name });

// Put whatever condition you want to put here   
query = query.Where(x => x.ID == 1);

foreach(var t1 in query)
{
      Console.WriteLine(t1.ID + " " + t1.Name);
}

Upvotes: 1

Related Questions