3

I have following method

public SomeObj LoadSomeData(int id)
{
    using (var context = new DataContext())
    {
        var result = 
            context.Database.SqlQuery<SomeObj>
            ($"SELECT * FROM SOMEOBJECT WHERE id = {id}")
        return result;
    }
}

This works fine, now the specification has changed, and I need to lookup multiple ids. I get ids as list of int and return a list. I read a bit about Sql Query and I can use IN and pass multiple values. So this is my changes:

public List<SomeObj> LoadSomeData(List<int> listOfIds)
{
    using (var context = new DataContext())
    {
        var result = 
            context.Database.SqlQuery<SomeObj>
            ($"SELECT * FROM SOMEOBJECT WHERE id IN ({id})") <-- I am not sure how to pass list in 
        return result;
    }
}

The second method works when I pass values manually to the query like 1,2,3 etc. but how can I pass list of to query?

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Maya Rager
  • 25
  • 2
  • 11

5 Answers5

6

This can be achieved different ways. I would suggest one way to go is using Join method.

var ids = string.Join(",", listOfIds)

This would return a list of {1, 2, 3} to string of 1, 2, 3

So putting that together with your final method would look like this:

public List<SomeObj> LoadSomeData(List<int> listOfIds)
{
    var ids = string.Join(",", listOfIds)
    using (var context = new KmdDataContext())
    {
        var result = 
            context.Database.SqlQuery<SomeObj>
            ($"SELECT * FROM SOMEOBJECT WHERE id IN ({Ids})")
        return result;
    }
}
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
4

You can pass them by generating the list:

var idList = string.Join(",", listOfIds);
$"SELECT * FROM SOMEOBJECT WHERE id IN ({idList})")

But if the list gets big (from memory, more than 2000 or so) that will fail. If you need more than that, use a Table-Valued Parameter - a bit more effort to set up, but they are very neat and scalable.

stuartd
  • 70,509
  • 14
  • 132
  • 163
3

Try String.Join(",", listofids) to convert the list into a comma separated string that you can use in the In part of the sql query

Ahmad.Tr
  • 756
  • 7
  • 22
2

If you already have a collection, you could simply do.

var ids = String.Join(",", parameters.Where(value => value > 5 && value < 10));

Now you would be able to pass the variable to query.

var query = $"SELECT * FROM ... WHERE Id IN ({ids})";
Greg
  • 11,302
  • 2
  • 48
  • 79
  • 1
    .. where do the values 5 and 10 come from? – stuartd Mar 28 '18 at 22:14
  • 1
    @stuartd Just used it as a basic where clause to filter the collection down to place the range. Assuming the list has more than he would like to actually query against. – Greg Mar 28 '18 at 22:20
1

Here is an approach using LINQ and Entity Framework. From your example, there is no apparent need to use raw SQL.

public List<SomeObj> LoadSomeData(List<int> listOfIds)
{
    using (var context = new DataContext())
    {
        return context.SomeObj.Where(x => listOfIds.Contains(x.id)).ToList();
    }
}

This is the equivalent to WHERE id in (1,2,3)

devlin carnate
  • 8,309
  • 7
  • 48
  • 82