locked
How to get a lookup to return a bool for a where clause? RRS feed

  • Question

  • Given:  2 Tables : Table1 and Table2

    Table1 contains one field of Table2 both named ID..

    where [Table1].ID == [Table2].Find(ID)]

    Table2 is a collection of things, I want to find that ID in the collection, but have the where clause compare the IDs (if found in Table2)..


    JP Cowboy Coders Unite!


    Monday, August 11, 2014 3:47 PM

Answers

  • Hello,

    Do not quite understand what you are trying to do.

    >>Table2 is a collection of things

    Is Table2  a collection of table2 entities as List<Table2>? If it is, the Find() method would return an entity rather than a collection of IDs, for getting its ID, having to write code as:

    db.Orders.ToList().Find(o => o.OrderID == 1).OrderID;

    >>//This does not work where Table2.Contains(Table1.ID)

    How does it not work for you? Does it throw any exception? As far as I know, if the Table2 is what I described above, it should accept an entity type. You could write code as below to firstly select its ID collection:

    db.Orders.ToList().Select(o => o.OrderID).Contains(Table1.ID);

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Mr. Javaman II Thursday, August 14, 2014 7:53 PM
    Tuesday, August 12, 2014 2:36 AM

All replies

  • //This does not work
    where Table2.Contains(Table1.ID)
    What I need is to match the field named ID in Table2 to Table1.ID.

    JP Cowboy Coders Unite!


    Monday, August 11, 2014 3:50 PM
  • Hello,

    Do not quite understand what you are trying to do.

    >>Table2 is a collection of things

    Is Table2  a collection of table2 entities as List<Table2>? If it is, the Find() method would return an entity rather than a collection of IDs, for getting its ID, having to write code as:

    db.Orders.ToList().Find(o => o.OrderID == 1).OrderID;

    >>//This does not work where Table2.Contains(Table1.ID)

    How does it not work for you? Does it throw any exception? As far as I know, if the Table2 is what I described above, it should accept an entity type. You could write code as below to firstly select its ID collection:

    db.Orders.ToList().Select(o => o.OrderID).Contains(Table1.ID);

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Mr. Javaman II Thursday, August 14, 2014 7:53 PM
    Tuesday, August 12, 2014 2:36 AM
  • Let me get back to you on this one.

    JP Cowboy Coders Unite!

    Wednesday, August 13, 2014 1:29 PM
  • This problem was due to my Model not matching the DB!  I attempted to add relationships in the model that were not in the db.  This caused all kinds of issues and I think this was just one of them.

    Don't add relationships to the model that will not be updated in the databse.  If you cannot update the database to get proper contraints, you must use something like LINQ Joins to get what you want.  If you want a strongly typed resultset, then you have to create one.  NOTE:  Starting with DBContext TT templates, they do create POCO classes. You can use one of them (The one with the most fields) to be a base class and then just add a few of the fields for the projection of other table fields due to the join. 

    Whew finally figured it out!


    JP Cowboy Coders Unite!

    Thursday, August 14, 2014 7:53 PM