0

I have one database table name as:DrippingPerformer

Fields:

DrippingPerformerId,ActivistId,username
1   11   abc
2   11   abc
3   12   xyz
4   13   lmn
5   14   lmn

Table 2:Dripping

ActivistId,name

11   rr
12   tt
13   uu
14   pp

Now i want to get all DrippingPerformer Whose ActivistId is 11,12,13.

I have created 1 view for this:

CREATE VIEW [dbo].[View1]
AS
SELECT   dbo.DrippingPerformer.DrippingPerformerId,dbo.DrippingPerformer.ActivistId,                    
FROM dbo.DrippingPerformer INNER JOIN  dbo.Dripping ON dbo.DrippingPerformer.ActivistId = dbo.Dripping.ActivistId

Now from this i want to get all DrippingPerformer Whose ActivistId is 11,12,13.

This is my code:

    public IQueryable GetData(string Ids)
            {
//Here id contains 11,12,13.
                   List<int> actions = Ids.Split(',').Select(int.Parse).ToList();
                    var context = new DataContext();
                    var q = from g in context.MyView
                            where
                                actions.Contains((int)g.ActivistId)
                            select g;
                    return q;
            }

But here in above query i dont want to use contains.So is there any better alternative than this??

can anybody provide me better alternative than this??

Albireo
  • 10,977
  • 13
  • 62
  • 96
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 1
    Your solution looks perfectly fine, and there are several similar questions... what would constitute a "better" alternative for you? – Paolo Tedesco Dec 15 '15 at 10:02
  • 1
    `Ids.Split(',').Select(int.Parse).ToList()` for the love of Zalgo, use a IEnumerable ids` as the `GetData` parameter. – Albireo Dec 15 '15 at 10:08
  • @PaoloTedesco:Can you tell me how this part like actions.Contains((int)g.ActivistId) will be transformed in to sql query? – I Love Stackoverflow Dec 15 '15 at 10:14
  • @Albireo:Can you tell me how this part like actions.Contains((int)g.ActivistId) will be transformed in to sql query? – I Love Stackoverflow Dec 15 '15 at 10:14
  • Have you read the question elected as "duplicate master"? It's explained. – Albireo Dec 15 '15 at 10:16
  • `Contains` gets translated to a concatenation of `Or` clauses in sql because Linq-to-sql doesn't contain anything to translate sql `In`. That's why contains is very bad for big collections. If you're really looking for performance, you're best off using an sql-query afaik (possible to execute sql queries within entity framework!) – Alexander Derck Dec 15 '15 at 10:16
  • https://msdn.microsoft.com/en-us/data/jj592907.aspx – Alexander Derck Dec 15 '15 at 10:17
  • @Albireo:Ok i have got you but is there any better way than this that is what i am asking for.I mean for searching this list of id`s in my DrippingPerformer table – I Love Stackoverflow Dec 15 '15 at 10:20
  • Quoting @PaoloTedesco, *"Your solution looks perfectly fine, and there are several similar questions... **what would constitute a "better" alternative for you?**"* – Albireo Dec 15 '15 at 10:22

0 Answers0