我有一个用户列表,每个用户都有一些喜欢的产品。所以每个用户都有一个最喜欢的产品列表。
在搜索面板中我想检查一些产品,并且作为搜索结果,我想返回至少拥有其中一个已检查产品的用户列表。最后一个。但没有检查任何这些产品的用户。
public class User
{
public long Id { get; set; }
public string Name { get; set; }
public List<UserProduct> UserProducts { get; set; }
public User()
{
}
public User(long id,string name,List<long> productIds)
{
Id = id;
Name = name;
UserProducts = productIds.Select(x => new UserProduct(id, x)).ToList();
}
}
public class Products
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public Products()
{
}
public Products(int productId,string name)
{
ProductId = productId;
ProductName = name;
}
}
public class UserProduct
{
public int Id { get; set; }
public long UserId { get; set; }
public long ProductId { get; set; }
public UserProduct()
{
}
public UserProduct(long userId,long productId)
{
ProductId = productId;
UserId = userId;
}
}
public class SearchDto
{
public List<long> SearchProductIds { get; set; }
}
public class Main
{
public void FillUsers()
{
List<Products> products=new List<Products>()
{
new Products(1,"a"),
new Products(2,"b"),
new Products(3,"c"),
new Products(4,"d"),
new Products(5,"e"),
new Products(6,"f"),
new Products(7,"g"),
};
List<User> users=new List<User>()
{
new User(1,"jack",new List<long>(){1} ),
new User(2,"Mary",new List<long>(){1,4} ),
new User(3,"Sam",new List<long>(){5} ),
new User(4,"Sara",new List<long>(){7,1,2} ),
};
SearchDto dto=new SearchDto()
{
SearchProductIds = new List<long> { 1,4}
};
//Here as search dto :I want to get Jack,Mary and Sara beacause they have 1 or 4 in their products
}
}
答案 0 :(得分:1)
假设您有一个所选产品的ID列表(selectedProductIdList
)
我假设你的
User
模型看起来像这样
public string Username {get; set;}
public string FirstName {get; set;}
//....
//more User data
//....
public List<Product> FavouriteProduct {get; set;}
您的用户搜索结果将是
var userList = listOfUsers.Where(u => u.FavouriteProduct.Where(p => selectedProductIdList.Contains(p.Id)));
答案 1 :(得分:0)
假设所有列表都在内存中,以防数据库相应地修改代码。
var result= (from p1 in SearchDtoList
join p2 in UserProductList
on p1 equals p2.ProductId
join u in UserList
on p2.UserId equals u.Id
select u).ToList();
答案 2 :(得分:0)
如果SearchPanelDto.SelectedProductsList返回ID列表,则
//Get selected productIds from search panel
Var searchedProductsIds = SearchPanelDto.SelectedProductsList.select(m=>m.ProductId).ToList();
var userList = listOfUsers.Where(m =>
searchedProductsIds.Any(x=> m.IntrestetProducts.Select(t => t.ProductId).Any(z => x.Equals(z))));
否则,如果SearchPanelDto.SelectedProductsList返回包含Id的对象列表
java.time