2012-02-23 68 views
1

我有一个多对多的关系如下:LINQ结果匹配的所有关系

产品表:

ProductID 
Description 

产品属性表:

FeatureID 
ProductID 
FeatureValue 

所有产品可以有许多功能。 我有一个要素类

public class Feature 
{ 
    public Guid FeatureID { get; set; } 
    public string FeatureValue { get; set; } 
} 

和功能列表,来自搜索

List<Feature> searchFeaturesList = new List<Feature>(); 

foreach (string var in Request.QueryString) 
{ 
    searchFeaturesList.Add(new Feature { FeatureID = new Guid(var.ToString()), FeatureValue = Request.QueryString[var] }); 
} 

我怎样才能匹配所有的特征ID和特征值是对所有产品的列表列表使用LINQ to SQL。 我试过使用Contains,但是我得到了与所有功能匹配的所有产品的结果。我需要它来匹配所有的功能。 请注意,每个FeatureID的可以有不同的FeatureValue 谢谢

+1

我们展示你的代码,你尝试过使用包含。这可能只是一个需要应用的小变化。 – dtb 2012-02-23 14:17:13

回答

2
var query = Products.AsQueryable(); 

foreach (var feature in searchFeaturesList) 
{ 
    // create here new values of featureId and featureValue otherwise will get the first ones from the iterator 
    var featureId = feature.FeatureID; 
    var featureValue = feature.FeatureValue; 
    // each feature id and value is tested if exists in the ProductFeatures 
    query = query.Where(p=>p.ProductFeatures 
          .Any(pf=> pf.FeatureID == featureId && pf.FeatureValue == featureValue)); 
} 
+1

很好用!谢谢! 我不明白为什么你必须创建featureId和featureValue的新值。但是,如果你不像你指出的那样做,那肯定不行。 – vts 2012-02-23 15:01:17

+0

http://msdn.microsoft.com/en-us/library/bb882641.aspx – 2012-02-23 15:12:39

相关问题