asp.net-mvc – LINQ Distinct()
发布时间:2020-12-16 00:46:11 所属栏目:asp.Net 来源:网络整理
导读:我正在努力在LINQ中获取此sql查询的结果 SELECT DISTINCT(Type)FROM ProductWHERE categoryID = @catID 这是我的存储库查询: public IQueryableProdInfo GetProdInfo() { var data = from u in db.Prod select new ProdInfo { PID = u.PID,CatID = u.CatID,
我正在努力在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(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容