Asp.net mvc 3 使用LINQ返回属于列表中所有标签的产品
我有一个服务方法,它接受一个带分隔符的标记列表,并且应该返回一个分配给该列表中所有标记的产品列表 这就是我拥有的,它不返回任何产品。我仔细检查了数据,有一个产品属于两个标签Asp.net mvc 3 使用LINQ返回属于列表中所有标签的产品,asp.net-mvc-3,entity-framework,linq-to-entities,Asp.net Mvc 3,Entity Framework,Linq To Entities,我有一个服务方法,它接受一个带分隔符的标记列表,并且应该返回一个分配给该列表中所有标记的产品列表 这就是我拥有的,它不返回任何产品。我仔细检查了数据,有一个产品属于两个标签 public List<Product> GetTagProducts(string tags) { //list of parameters var tagParams = tags.Split('+').ToList(); //return all products which be
public List<Product> GetTagProducts(string tags)
{
//list of parameters
var tagParams = tags.Split('+').ToList();
//return all products which belong to ALL tags specified in tagParams list
return (from pt in _repository.ProductTags
where tagParams.All(p => p == pt.Tag.Name)
select pt.Product).Distinct().Take(75).ToList();
}
public class Tag
{
[Key]
public int TagId { get; set; }
public string Name { get; set; }
public virtual List<Product> Products { get; set; }
public virtual List<ProductTag> ProductTags { get; set; }
}
public class Product
{
public int ProductId { get; set; }
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Short Description")]
public string ShortDescription { get; set; }
[Required]
[Display(Name = "Long Description")]
public string LongDescription { get; set; }
[Required]
[Display(Name = "Price")]
public decimal Price { get; set; }
public virtual List<Tag> Tags { get; set; }
}
public class ProductTag
{
[Key]
public int ProductTagId { get; set; }
[ForeignKey("Product")]
public int ProductId { get; set; }
[ForeignKey("Tag")]
public int TagId { get; set; }
public virtual Product Product { get; set; }
public virtual Tag Tag { get; set; }
}
//Repository
private DatabaseContext _context = new DatabaseContext();
public IQueryable<ProductTag> ProductTags
{
get { return _context.ProductTags; }
}
假设我们有以下产品:
product tags
------- ----
Wipers Automotive, General
Air Freshener General
Gloves General
Tires Automotive
Mirror Automotive, General
查询应返回“雨刮器”和“镜像”。方法链样式:
List<Product> allProducts = GetAllProductsFromSomewhere();
allProducts.Where(p => tagParams.All(tag =>
p.Tags.Select(x => x.Name).Contains(tag))).Distinct().Take(75).ToList();
List allProducts=getallproductsfromsomeone();
allProducts.Where(p=>tagParams.All(tag=>
p、 Tags.Select(x=>x.Name).Contains(tag)).Distinct().Take(75.ToList();
All
表示所有标记都应等于一个标记。你说它包含两个标签,所以这是不可能的。简言之: 确定序列的所有元素是否满足条件 方法链样式:
List<Product> allProducts = GetAllProductsFromSomewhere();
allProducts.Where(p => tagParams.All(tag =>
p.Tags.Select(x => x.Name).Contains(tag))).Distinct().Take(75).ToList();
List allProducts=getallproductsfromsomeone();
allProducts.Where(p=>tagParams.All(tag=>
p、 Tags.Select(x=>x.Name).Contains(tag)).Distinct().Take(75.ToList();
All
表示所有标记都应等于一个标记。你说它包含两个标签,所以这是不可能的。简言之: 确定序列的所有元素是否满足条件
谢谢gdoron,但是他的没有返回正确的结果。它返回属于任何标签的产品,而不是属于所有标签的产品。我编辑了我的问题,以澄清我需要的结果。@JohnL。同时展示课程。
\u repository.ProductTags
看起来像什么?@JohnL。我仍然不知道\u存储库
看起来像什么,但应该是更新后的答案。很抱歉,我误解了评论。我已经更新了我的帖子。存储库代码位于代码列表的底部。@JohnL。只需将allProducts
更改为从存储库中获取产品列表的方式即可。谢谢gdoron,但他不会返回正确的结果。它返回属于任何标签的产品,而不是属于所有标签的产品。我编辑了我的问题,以澄清我需要的结果。@JohnL。同时展示课程。\u repository.ProductTags
看起来像什么?@JohnL。我仍然不知道\u存储库
看起来像什么,但应该是更新后的答案。很抱歉,我误解了评论。我已经更新了我的帖子。存储库代码位于代码列表的底部。@JohnL。只需将allProducts
更改为从存储库中获取产品列表的方式。对,更新问题以显示类。谢谢。对了,更新了问题来展示课堂。谢谢