Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# LINQ Entities
Hello,
 
I am learning Linq to Entities newly. I wrote a function that fetcehs records from sql server contains of origin values.
 
Table Practices contains the following fields.
 
PracticeID Autoincrement,
PracticeName nvarchar,
etc.
 
I wanted to get a list of practices with integer similare to the below query.
 
Select PracticeName from practices Where PracticeID in (1, 5, 6)
 
for this i wrote the following code.
 
GetPractices(string practices)
{
AuthenticationEntities authEntity = new AuthenticationEntities();
 
                   List<Practices> prac = (from p in authEntity.Practices
                                           where p.Practiced.Contains(Practices)
                                           select new Practices
                                           {
                                               Practice = p.PracticeName
                                           }).ToList<Practices>();
 
}
 

I am unable to understand the error, it is not showing . contains in intellisense and error showing as Delegate 'System.Func<storedb.practice,int,bool>' does not take 1 arguments
 

Kinldy help.
 
Thanks in advance,
 
chowdary.
Posted 30-Aug-12 22:56pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If you have PracticeID as an int you should be working with int's IMO.
 
This is how I would do it
 
Create a console app and paste the following code:
 
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace Linq_Contains_Any
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] idsToBeSearched = new int[] { 1, 3, 4 };
            List<practice> Practices = new List<practice>();
 
            Practices.Add(new Practice { PracticeID = 1, PracticeName = "AAAAAAAA" });
            Practices.Add(new Practice { PracticeID = 2, PracticeName = "BBBBBBBB" });
            Practices.Add(new Practice { PracticeID = 3, PracticeName = "CCCCCCCC" });
            Practices.Add(new Practice { PracticeID = 4, PracticeName = "DDDDDDDD" });
            Practices.Add(new Practice { PracticeID = 5, PracticeName = "EEEEEEEE" });
            Practices.Add(new Practice { PracticeID = 6, PracticeName = "FFFFFFFF" });
            Practices.Add(new Practice { PracticeID = 7, PracticeName = "GGGGGGGG" });
            Practices.Add(new Practice { PracticeID = 8, PracticeName = "HHHHHHHH" });
            Practices.Add(new Practice { PracticeID = 9, PracticeName = "IIIIIIII" });
            Practices.Add(new Practice { PracticeID = 10, PracticeName = "JJJJJJJJ" });
 
            var result = from p in Practices
                         where idsToBeSearched.Any(id => p.PracticeID == id)
                     select p;
 
            foreach (var item in result)
            {
                Console.WriteLine(string.Format("ID {0} - {1}", item.PracticeID, item.PracticeName));
            }
 
            Console.ReadLine();
        }
    }
 
    public class Practice
    {
        public int PracticeID { get; set; }
        public string PracticeName { get; set; }
    }   
 
}
</practice></practice>
 
The result output will be
 
ID 1 - AAAAAAAA
ID 3 - CCCCCCCC
ID 4 - DDDDDDDD
  Permalink  
Comments
Matt T Heffron at 1-May-14 12:50pm
   
I don't know why someone would downvote this.
This is exactly the correct approach.
The PracticeID is int (Autoincrement) so use int when comparing with it.
If there are still issues with the Linq to SQL trying to convert the where condition to the underlying SQL query, the approach of using int vs. string is correct.
 
My +5!
Edson_Ferreira at 1-May-14 13:31pm
   
Thanks mate 😊
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

please try this, i got this working
 

string[] idsToBeSearched = new string[] { "1", "3" };
             List<Collection> interestedData= (from p in projectList
                        where idsToBeSearched.Contains(p.projID.ToString())
                        select new Collection
                        {
     ProjectName=p.ProjectName
 

                        }).ToList<Collection>();
  Permalink  
Comments
chowdary2005 at 31-Aug-12 7:10am
   
Hari,
 
I have modified my code as the following. I have tries both string [] and string but getting an error as "LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."
 

 
string[] Practices = users.ElementAt(0).Practices.Split(new char[] { ',' });
 
string pras = null;
 
for (int i = 0; i < Practices.Count(); i++)
{
if (pras == null)
{
pras = Practices[i].ToString();
}
else
{
pras = pras + "," + Practices[i].ToString();
}
}

 
AuthenticationEntities authEntity = new AuthenticationEntities();
 

 
List<Practices> prac = (from p in authEntity.Practices
where pras.Contains (p.PracticeID.ToString())
select new Practices
{
Practice = p.PracticeName
}).ToList<Practices>();
 
Kindly help.
harikakakkireni at 31-Aug-12 7:41am
   
Chowdary,
 
can you tell me what actually is present in Practices?
 
string[] Practices = users.ElementAt(0).Practices.Split(new char[] { ',' });
 
this above line of code does it return you all the id's?
 
if yes,
 
then you need not make string [] as string by appending it with ,
 
just try this
 
string[] Practices = users.ElementAt(0).Practices.Split(new char[] { ',' });

 
AuthenticationEntities authEntity = new AuthenticationEntities();
 

 
List<Practices> prac = (from p in authEntity.Practices
where Practices.Contains (p.PracticeID.ToString())
select new Practices
{
Practice = p.PracticeName
}).ToList<Practices>();
 

 
chowdary2005 at 31-Aug-12 7:46am
   
Hari, In practices table i have two fields 1) PracticeID autoincrement, 2) practiceName nvarchar, 3) practices nvarchar 4) userName nvarchar Practices [3rd fields above] contains value as 1, 3, 6. When user logsin [using username] i am getting this field into string[] practices. Then i am trying to pass this to another table PracticeNames table contains all practices enrolled with. Hence need to get all the rows with string [] practices containing practiceIDs. Hope explained clearly. chowdary.
harikakakkireni at 31-Aug-12 7:57am
   
Chowdary,
i am not sure if you are able to understand it correctly,
let me change the variable names little and try to edit the code
 
string[] allPracticeIDsOfUser = users.ElementAt(0).Practices.Split(new char[] { ',' });

 
AuthenticationEntities authEntity = new AuthenticationEntities();
 

 
List<Practices> prac = (from p in authEntity.Practices
where allPracticeIDsOfUser.Contains (p.PracticeID.ToString())
select new Practices
{
Practice = p.PracticeName
}).ToList<Practices>();
 
Hope this helps
 
chowdary2005 at 31-Aug-12 8:00am
   
Harish,
 
Actually string[] practices is a string but i am trying to query practiceID with autoincrement ID which is int may be it is issue???
 
chowdary.
harikakakkireni at 31-Aug-12 8:05am
   
where allPracticeIDsOfUser.Contains (p.PracticeID.ToString())
 
PracticeID which is int gets converted to string when you say PracticeID.ToString() and is checked against string []practices
 
practices.Contains(p.PracticeID.ToString())
select new Practices
{
Practice = p.PracticeName
}).ToList<Practices>();
 
So thats not an issue at all,
what error are you facing now, after you changing your solution
chowdary2005 at 31-Aug-12 20:24pm
   
Same error , "LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."
 
This forum does not contain to attach image, else i might have attache the image for your review.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 359
1 _Amy 230
2 Manfred R. Bihy 200
3 CHill60 180
4 OriginalGriff 175
0 OriginalGriff 7,495
1 Sergey Alexandrovich Kryukov 6,402
2 Maciej Los 3,849
3 Peter Leow 3,568
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 1 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100