在Linq到实体中使用包含运算符 [英] Using Contains Operator in Linq to Entities
问题描述
您好,
我正在学习Linq to Entities。我写了一个函数,从sql server中取出包含原始值的记录。
表格实践包含以下字段。
PracticeID Autoincrement,
PracticeName nvarchar,
等
我想得到一份清单对下面的查询进行整数相似的练习。
从练习中选择练习名练习ID在(1,5,6)中
为此我编写了以下代码。
GetPractices(字符串练习)
{
AuthenticationEntities authEntity = new AuthenticationEntities();
列表< 惯例 > prac =(来自p in authEntity.Practices
其中p.Practiced.Contains(Practices)
选择新的实践
{
Practice = p.PracticeName
})。ToList < 实践 > ();
}
我无法理解错误,它没有显示。包含intellisense和错误显示为委托'System.Func< storedb.practice,int,bool>'不带1个参数
Kinldy的帮助。
提前致谢,
chowdary。
如果您将PracticeID作为int,那么您应该使用int的IMO。
这就是我要做的事情
创建一个控制台应用程序并粘贴以下代码:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
命名空间 Linq_Contains_Any
{
class Program
{
静态 void Main( string [] args)
{
int [] idsToBeSearched = new int [] { 1 , 3 , 4 };
列表< practice> Practices = new List< practice>();
Practices.Add( new 练习{PracticeID = 1 ,PracticeName = < span class =code-string> AAAAAAAA});
Practices.Add( new 练习{PracticeID = 2 ,PracticeName = BBBBBBBB});
Practices.Add( new 练习{PracticeID = 3 ,PracticeName = CCCCCCCC});
Practices.Add( new 练习{PracticeID = 4 ,PracticeName = DDDDDDDD});
Practices.Add( new 练习{PracticeID = 5 ,PracticeName = EEEEEEEE});
Practices.Add( new 练习{PracticeID = 6 ,PracticeName = FFFFFFFF});
Practices.Add( new 练习{PracticeID = 7 ,PracticeName = GGGGGGGG});
Practices.Add( new 练习{PracticeID = 8 ,PracticeName = HHHHHHHH});
Practices.Add( new 练习{PracticeID = 9 ,PracticeName = IIIIIIII});
Practices.Add( new 练习{PracticeID = 10 ,PracticeName = JJJJJJJJ});
var result = 来自 p in 实践
其中 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 练习
{
public int PracticeID {获得跨度>; set ; }
public string PracticeName { get 跨度>; set ; }
}
}
< / practice > < / practice < span class =code-keyword>>
结果输出将是
ID 1 - AAAAAAAA
ID 3 - CCCCCCCC
ID 4 - DDDDDDDD
请尝试这个,我有这个工作
string [] idsToBeSearched = new string [] {1 ,3};
列表< 集合 > interestedData =(from project in projectList
where idsToBeSearched.Contains(p.projID.ToString())
select new Collection
{
ProjectName = p.ProjectName
})。ToList < 集合 > 跨度>();
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.
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
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>();
这篇关于在Linq到实体中使用包含运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!