How can I make one query out of many others
-
I've this code: (it's much longer...)
if (selection == "bild" )
{
erg = from item in _db.object_1
where item.article_number != null
&& item.bild == null
&& item.brand.StartsWith(marke)
&& ( item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten))
select item.article_number;
}
else if (selection == "machine_group")
{
erg = from item in _db.object_1
where item.article_number != null
&& item.machine_group == null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten))
select item.article_number;
}How can I shorten it? Can I get a Variable for for example "item.bild"? Its always the same... if the selection is "bild" I will ask if "item.bild" is null; if the selection is "machine_group" I will ask if "item.machine_group" is null Can you help me?
-
I've this code: (it's much longer...)
if (selection == "bild" )
{
erg = from item in _db.object_1
where item.article_number != null
&& item.bild == null
&& item.brand.StartsWith(marke)
&& ( item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten))
select item.article_number;
}
else if (selection == "machine_group")
{
erg = from item in _db.object_1
where item.article_number != null
&& item.machine_group == null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten))
select item.article_number;
}How can I shorten it? Can I get a Variable for for example "item.bild"? Its always the same... if the selection is "bild" I will ask if "item.bild" is null; if the selection is "machine_group" I will ask if "item.machine_group" is null Can you help me?
You can extract the common part of the query, and apply the additional filters to that common part:
var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten));if (selection == "bild" || selection == "Alle")
{
erg = from item in query
where item.bild == null
select item.article_number;
}
else if (selection == "machine_group") // || selection == "Alle")
{
erg = from item in query
where item.machine_group == null
select item.article_number;
}NB: There's no point having
selection == "Alle"
in yourelse if
test. Ifselection == "Alle"
, your code will execute theif
branch, and ignore theelse if
branch. If you've got a lot of branches, you should probably look at using aswitch
block:var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten));switch (selection)
{
case "bild":
case "Alle":
{
erg = from item in query
where item.bild == null
select item.article_number;
break;
}
case "machine_group":
{
erg = from item in query
where item.machine_group == null
select item.article_number;
break;
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You can extract the common part of the query, and apply the additional filters to that common part:
var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten));if (selection == "bild" || selection == "Alle")
{
erg = from item in query
where item.bild == null
select item.article_number;
}
else if (selection == "machine_group") // || selection == "Alle")
{
erg = from item in query
where item.machine_group == null
select item.article_number;
}NB: There's no point having
selection == "Alle"
in yourelse if
test. Ifselection == "Alle"
, your code will execute theif
branch, and ignore theelse if
branch. If you've got a lot of branches, you should probably look at using aswitch
block:var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten));switch (selection)
{
case "bild":
case "Alle":
{
erg = from item in query
where item.bild == null
select item.article_number;
break;
}
case "machine_group":
{
erg = from item in query
where item.machine_group == null
select item.article_number;
break;
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I know.. the "else if" have to be an "if" please ignor it Thank you thats much shorter... but maybe its going shorter?
-
I know.. the "else if" have to be an "if" please ignor it Thank you thats much shorter... but maybe its going shorter?
-
Thank you, but I do not have experience in dynamic Linq, and i don't have time to study it now, isn't there any other way?
-
Thank you, but I do not have experience in dynamic Linq, and i don't have time to study it now, isn't there any other way?
No, there isn't and Dynamic LINQ isn't hard - you could have learned it in the time you waited for my reply. Honestly, it looks like you didn't even read the link I put in the previous post - if you had, you'd have seen that you can mix and match your standard "
from x in ....
" syntax with Dynamic LINQ, so you only need to touch the one place. -
I know.. the "else if" have to be an "if" please ignor it Thank you thats much shorter... but maybe its going shorter?
Not sure it's technically shorter, but you could build a dictionary to map your
selection
variable to a filter, and store that map as a private field in your class:private static readonly Dictionary<string, Expression<Func<object_1, bool>>> Filters = new Dictionary<string, Expression<Func<object_1, bool>>>
{
{ "bild", item => item.bild == null },
{ "machine_group", item => item.machine_group == null },
};You would then use that dictionary to look up the filter:
var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten));Expression<Func<object_1, bool>> filter;
if (Filters.TryGetValue(selection, out filter))
{
query = query.Where(filter);
}erg = query.Select(item => item.article_number);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Not sure it's technically shorter, but you could build a dictionary to map your
selection
variable to a filter, and store that map as a private field in your class:private static readonly Dictionary<string, Expression<Func<object_1, bool>>> Filters = new Dictionary<string, Expression<Func<object_1, bool>>>
{
{ "bild", item => item.bild == null },
{ "machine_group", item => item.machine_group == null },
};You would then use that dictionary to look up the filter:
var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten));Expression<Func<object_1, bool>> filter;
if (Filters.TryGetValue(selection, out filter))
{
query = query.Where(filter);
}erg = query.Select(item => item.article_number);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you that looks good, i will try it.
-
Not sure it's technically shorter, but you could build a dictionary to map your
selection
variable to a filter, and store that map as a private field in your class:private static readonly Dictionary<string, Expression<Func<object_1, bool>>> Filters = new Dictionary<string, Expression<Func<object_1, bool>>>
{
{ "bild", item => item.bild == null },
{ "machine_group", item => item.machine_group == null },
};You would then use that dictionary to look up the filter:
var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten));Expression<Func<object_1, bool>> filter;
if (Filters.TryGetValue(selection, out filter))
{
query = query.Where(filter);
}erg = query.Select(item => item.article_number);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Okay now I get a few errors... 1.:
A query body must end with a select clause or a group clause
2.:
'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' has some invalid arguments
3.:
The type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'.
4.:
The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
5.:
Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable' to 'System.Linq.IQueryable'
-
Okay now I get a few errors... 1.:
A query body must end with a select clause or a group clause
2.:
'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' has some invalid arguments
3.:
The type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'.
4.:
The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
5.:
Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable' to 'System.Linq.IQueryable'
I suspect they're all related. Try adding a
select
clause to the end of the first query:var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten))
select item;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I suspect they're all related. Try adding a
select
clause to the end of the first query:var query = from item in _db.object_1
where item.article_number != null
&& item.brand.StartsWith(marke)
&& (item.o_path.StartsWith(pathwerkzeug) || item.o_path.StartsWith(pathgarten))
select item;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks ;) it WAS working ... and then I changed it for all and now theres this errro:
System.TypeInitializationException: Der Typeninitialisierer für "Quälix.AbfrageIsNull" hat eine Ausnahme verursacht. ---> System.ArgumentException: Ein Element mit dem gleichen Schlüssel wurde bereits hinzugefügt.
bei System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
bei System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
bei System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
bei Quälix.AbfrageIsNull..cctor() in q:\PIM_PolicingTool\Quälix\Quälix\AbfrageIsNull.cs:Zeile 12.
--- Ende der internen Ausnahmestapelüberwachung ---
bei Quälix.AbfrageIsNull.ArtikelAufNullPruefen(String selection, List`1 marken)
bei Quälix.Form1.btAbfragIsNullSuche_Click(Object sender, EventArgs e) in q:\PIM_PolicingTool\Quälix\Quälix\Form1.cs:Zeile 56.this is my code:
if (chbAbfrageArtikel.Checked == true)
{
endErgebnis += "ARTIKEL:";
endErgebnis += Environment.NewLine;
string zwischenErg = AbfrageIsNull.ArtikelAufNullPruefen(selection, marken);if (zwischenErg == "") { endErgebnis += "Alles zugeordnet"; } else { endErgebnis += zwischenErg; } endErgebnis += Environment.NewLine; endErgebnis += Environment.NewLine; } if (chbAbfrageZubehoer.Checked == true) { endErgebnis += "ZUBEHÖR:"; endErgebnis += Environment.NewLine; string zwischenErg = "";// AbfrageIsNull.ZubehoerAufNullPruefen(selection, marken); if (zwischenErg == "") { endErgebnis += "Alles zugeordnet"; } else { endErgebnis += zwischenErg; } endErgebnis += Environment.NewLine; endErgebnis += Environment.NewLine; } if (chbAbfrageErsatzteil.Checked == true) { endErgebnis += "ERSATZTEILE:"; endErgebnis += Environment.NewLine; string zwischenErg = "";// AbfrageIsNull.ErsatzteilAufNullPruefen(selection, marken); if (zwischenErg == "") { endErgebnis += "Alles zugeordnet"; } else { endErgebnis += zwischenErg; }
-
Thanks ;) it WAS working ... and then I changed it for all and now theres this errro:
System.TypeInitializationException: Der Typeninitialisierer für "Quälix.AbfrageIsNull" hat eine Ausnahme verursacht. ---> System.ArgumentException: Ein Element mit dem gleichen Schlüssel wurde bereits hinzugefügt.
bei System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
bei System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
bei System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
bei Quälix.AbfrageIsNull..cctor() in q:\PIM_PolicingTool\Quälix\Quälix\AbfrageIsNull.cs:Zeile 12.
--- Ende der internen Ausnahmestapelüberwachung ---
bei Quälix.AbfrageIsNull.ArtikelAufNullPruefen(String selection, List`1 marken)
bei Quälix.Form1.btAbfragIsNullSuche_Click(Object sender, EventArgs e) in q:\PIM_PolicingTool\Quälix\Quälix\Form1.cs:Zeile 56.this is my code:
if (chbAbfrageArtikel.Checked == true)
{
endErgebnis += "ARTIKEL:";
endErgebnis += Environment.NewLine;
string zwischenErg = AbfrageIsNull.ArtikelAufNullPruefen(selection, marken);if (zwischenErg == "") { endErgebnis += "Alles zugeordnet"; } else { endErgebnis += zwischenErg; } endErgebnis += Environment.NewLine; endErgebnis += Environment.NewLine; } if (chbAbfrageZubehoer.Checked == true) { endErgebnis += "ZUBEHÖR:"; endErgebnis += Environment.NewLine; string zwischenErg = "";// AbfrageIsNull.ZubehoerAufNullPruefen(selection, marken); if (zwischenErg == "") { endErgebnis += "Alles zugeordnet"; } else { endErgebnis += zwischenErg; } endErgebnis += Environment.NewLine; endErgebnis += Environment.NewLine; } if (chbAbfrageErsatzteil.Checked == true) { endErgebnis += "ERSATZTEILE:"; endErgebnis += Environment.NewLine; string zwischenErg = "";// AbfrageIsNull.ErsatzteilAufNullPruefen(selection, marken); if (zwischenErg == "") { endErgebnis += "Alles zugeordnet"; } else { endErgebnis += zwischenErg; }
The error message is quite clear - "An item with the same key has already been added". In your dictionary initializer, you have added the key
"Inventurart"
twice - the 6th and 29th items. Remove one of those items.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer