Cannot make WHERE IN clause working properly in LINQ to Entities
Posted: Fri 16 Jun 2017 05:41
Hello.
I'm trying to select items from database with List in a “WHERE” condition.
Assume we have the following entities:
I want to get messages posted by two users. I've tried the following code:
That doesn't work (0 items returned while 2 exists in database);
I've tried even the following approach:
It does't work also.
What's wrong with my code?
I use dotConnect for SQLite 5.3.583.
Sample project I've uploaded here
Click AddUsersAndMessages() button first to create database (on disk D) and then click ReadMessages() button (it uses all code above).
I'm trying to select items from database with List in a “WHERE” condition.
Assume we have the following entities:
Code: Select all
public class Message
{
public Guid Id { get; set; }
public String Text { get; set; }
public bool IsPrivate { get; set; }
public Guid UserId { get; set; }
}
public class User
{
public Guid Id { get; set; }
public String Name { get; set; }
public int Age { get; set; }
}
Code: Select all
messages = ActiveContext.Messages.Where(m => listOfUserIds.Contains(m.UserId)).ToList();
I've tried even the following approach:
Code: Select all
var guid1 = listOfUserIds[0];
var query = ActiveContext.Messages.Where(m => m.UserId.Equals(guid1));
var guid2 = listOfUserIds[1];
query = query.Concat(ActiveContext.Messages.Where(m => m.UserId.Equals(guid2)));
messages = query.ToList();
What's wrong with my code?
I use dotConnect for SQLite 5.3.583.
Sample project I've uploaded here
Click AddUsersAndMessages() button first to create database (on disk D) and then click ReadMessages() button (it uses all code above).