Click here to Skip to main content
15,436,734 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.

XML
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

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:

C#
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
 
Share this answer
 
Comments
Matt T Heffron 1-May-14 12:50pm     CRLF
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 1-May-14 13:31pm    
Thanks mate 😊
please try this, i got this working


XML
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>();
 
Share this answer
 
Comments
Charles Shob 31-Aug-12 7:10am     CRLF
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 31-Aug-12 7:41am     CRLF
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>();
Charles Shob 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 31-Aug-12 7:57am     CRLF
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
Charles Shob 31-Aug-12 8:00am     CRLF
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900