我正在努力在LINQ中获取此SQL查询的结果
SELECT disTINCT(Type) FROM Product WHERE categoryID = @catID
这是我的存储库查询:
public IQueryable<ProdInfo> GetProdInfo() { var data = from u in db.Prod select new ProdInfo { PID = u.PID,CatID = u.CatID,LastChanged = u.LastChanged,ChangedBy = u.ChangedBy,Type = u.Type,}; return data; }
过滤:
public static IQueryable<ProdInfo> GetdistinctProdType(this IQueryable<ProdInfo> qry,int CatID) { return from p in qry where p.CatID.Equals(CatID) select p; }
我需要过滤器返回不同的prod类型?我该怎么办?
解决方法
就这样:
public static IQueryable<ProdType> GetdistinctProdType( this IQueryable<ProdInfo> query,int categoryId) { return (from p in query where p.CatID == categoryId select p.Type).distinct(); }
请注意,我已经更改了返回类型 – 它应该匹配任何类型的ProdInfo.Type。
如果查询表达式本身相当简单,您可能会发现使用整个查询的扩展方法更易读:
public static IQueryable<ProdType> GetdistinctProdType( this IQueryable<ProdInfo> query,int categoryId) { return query.Where(p => p.CatID == categoryId) .Select(p => p.Type) .distinct(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。