Linq join on two values

后端 未结 2 1870
無奈伤痛
無奈伤痛 2021-01-16 19:14

Suppose I have a list of {City, State}. It originally came from the database, and I have LocationID, but by now I loaded it into memory. Suppose I also have a t

相关标签:
2条回答
  • 2021-01-16 19:25

    Well, there isn't a lot more that you can do, as long as you rely on a table lookup, the only thing you can do to speed up things is to put an index on City and State.

    The linq statement has to translate into a valid SQL Statement, where "Any" would translate to something like :

    SELECT * FROM Restaurants where City in ('...all cities')
    

    I dont know if other ORM's give better performance for these types of scenarios that EF, but it might be worth investigating. EF has never had a rumor for being fast on reads.

    Edit: You can also do this:

    List<string> names = new List { "John", "Max", "Pete" };
    bool has = customers.Any(cus => names.Contains(cus.FirstName)); 
    

    this will produce the necessary IN('value1', 'value2' ...) functionality that you were looking for

    0 讨论(0)
  • 2021-01-16 19:44

    It seems to me that you need this:

    var establishments =
        from r in restaurants
        join l in locations.Where(x => x.LocationId == id)
            on new { r.City, r.State } equals new { l.City, l.State } into gls
        select r;
    
    0 讨论(0)
提交回复
热议问题