views:

149

answers:

3

Is there a better way to do this? I tried to loop over the partsToChange collection and build up the where clause, but it ANDs them together instead of ORing them. I also don't really want to explicitly do the equality on each item in the partsToChange list.

var partsToChange = new Dictionary<string, string> {
    {"0039", "Vendor A"},
    {"0051", "Vendor B"},
    {"0061", "Vendor C"},
    {"0080", "Vendor D"},
    {"0081", "Vendor D"},        
    {"0086", "Vendor D"},
    {"0089", "Vendor E"},
    {"0091", "Vendor F"},
    {"0163", "Vendor E"},
    {"0426", "Vendor B"},
    {"1197", "Vendor B"}
};

var items = new List<MaterialVendor>();
foreach (var x in partsToChange)
{
    var newItems = (
    from m in MaterialVendor 
    where 
        m.Material.PartNumber == x.Key 
        && m.Manufacturer.Name.Contains(x.Value)
    select m
    ).ToList();
    items.AddRange(newItems);
}

Additional info: I am working in LINQPad and this is a LinqToSql query. Here MaterialVendor is both a class and a DataContext Table.

Edit: LinqToSql details.

This seems to be the best method that I have found for both readability and reducing the complexity. It also has the added benefit of not having the collection type defined explicitly. That means I can vary what comes back with an anonymous type.

var predicate = PredicateBuilder.False<MaterialVendor>();

foreach (var x in partsToChange)
{
    var item = x;
    predicate = predicate.Or (m =>
        m.Material.PartNumber == item.Key 
        && m.Manufacturer.Name.Contains(item.Value));
}

var items = from m in MaterialVendor.Where(predicate)
    select m;
+8  A: 

[Edit] Even better, since partsToChange is a Dictionary:

var items = MaterialVendor.Where(m =>
                m.Manufacturer.Name.Contains(partsToChange[m.Material.PartNumber])
            ).ToList();
BlueRaja - Danny Pflughoeft
When trying to query the DB the following exception is thrown: Method 'System.String get_Item(System.String)' has no supported translation to SQL.
Jeremy Roberts
@Jeremy: Was your code posted in the question working? Does my [previous edit ](http://stackoverflow.com/revisions/642074bc-df24-4d8b-ae07-8418ae7a8537/view-source) work?
BlueRaja - Danny Pflughoeft
@BlueRaja - you can't cross-query a L2S table with any other data structure, other than a `IList.Contains(...)` which is converted to an `IN (...)`
ck
A: 

The where clause size doesn't really matter. The querying within a loop is the part that drives maintainability and performance down.

List<MaterialVendor> items = 
(
  from z in MaterialVendor
  let partKey = z.Material.PartNumber
  where partsToChange.ContainsKey(partKey)
  let partValue = partsToChange[partKey]
  where z.Manufacturer.Name.Contains(partValue)
  select z
).ToList();

Now that we know that linq to sql is involved... here's a mixed mode query.

List<string> keys = partsToChange.Keys.ToList();

List<MaterialVendor> items =  
( 
  from z in MaterialVendor 
  let partKey = z.Material.PartNumber 
  where keys.Contains(partKey)
  select new {MatVendor = z, Name = z.Manufacturer.Name, Key = partKey}
).AsEnumerable()
.Where(x => x.Name.Contains(partsToChange[x.partKey]))
.Select(x => x.MatVendor)
.ToList(); 
David B
When trying to query the DB the following exception is thrown: Method 'Boolean ContainsKey(System.String)' has no supported translation to SQL.
Jeremy Roberts
+2  A: 

Look into PredicateBuilder

This will allow you to build a Linq to sql expression within a loop, adding the clauses with AND / OR where necessary, then execute it once at the end.

ck