Linq 查询问题
我有这段代码,它从 Alleged Perpetrator 表中返回 caseID。该表还有一列“LastName”。我想搜索 caseID 并返回 LastName,但我不知道如何编码。我一直在微软网站上寻找 LINQ to SQL 示例,但仍然无法弄清楚。任何帮助将不胜感激!
肯
public static class AllegedPerpetratorRepository
{
public static IQueryable<AllegedPerpetrator> GetByCaseID(
this IQueryable<AllegedPerpetrator> source,
int caseID)
{
return (from s in source where s.CaseID.Equals(caseID) select s);
}
}
I have this code that returns a caseID from an Alleged Perpetrator table. This table also has a column "LastName". I want to search on caseID and return LastName but I don't know how to code it. I've been on the microsoft site looking for LINQ to SQL examples but still can't figure it out. Any help would be greatly appreciated!
Ken
public static class AllegedPerpetratorRepository
{
public static IQueryable<AllegedPerpetrator> GetByCaseID(
this IQueryable<AllegedPerpetrator> source,
int caseID)
{
return (from s in source where s.CaseID.Equals(caseID) select s);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最后的结尾应该是:
。 。 。
select s.LastName);
编辑:
Ahmed 的建议和 Jeroen 的修复:
The very end should be:
. . .
select s.LastName);
Edit:
Ahmed's suggestion and Jeroen's fix:
您是否使用 Visual Studio 映射器工具从数据库创建了 LINQ to SQL 类?
您可以添加“新项目”,然后根据您的数据库架构添加 LINQ to SQL 类。该工具将为您从表中生成类。
然后,您可以使用这些代表数据库列和表的类(每个表一个类)来使用 LINQ。
如果你用 google 搜索的话,有一些关于 LINQ to SQL 的很好的教程。
Have you created a LINQ to SQL class from your database using the Visual Studio mapper tool?
You can add a 'new item' and then add a LINQ to SQL class based on your database schema. The tool will generate the classes from the tables for you.
Then you can use these classes which represent your database columns and tables (one class per table) to use LINQ.
There's some good tutorials on LINQ to SQL if you google it.
CaseIdValue是你传入的=
CaseIdValue is what you pass in=
如果给定案例 ID 只有一条记录,则可以使用 Single 表达式
If there is one and only one record for the given case id you can use the Single expression