Linq查询问题

Ken*_*als 2 c# linq iqueryable linq-to-sql

我有这个代码从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); 
    }
}
Run Code Online (Sandbox Code Playgroud)

Pat*_*her 6

最终应该是:

...select s.LastName);

编辑:

艾哈迈德的建议和Jeroen的解决方案:

public static class AllegedPerpetratorRepository
{
    public static IEnumerable<string> GetByCaseID(
        this IQueryable<AllegedPerpetrator> source,
        int caseID)
    {
        return (from s in source where s.CaseID.Equals(caseID) select s.LastName); 
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 此外,方法的返回类型将更改为"LastName"的类型,可能是"字符串" (4认同)