c# - How do I build a Func dynamically? -


I have the following code, which I want to put in the function, so I do not have to copy / paste several times. If for each case - Block:

  if (sort == ExportSortOrder.CardType) {cards = cards.OrderBy (f => f.CardType); Foreign currency (card type type Enum.GetValues ​​(typef (cardtype)) (if (! Card! Any (f = & gt; f.CardType == type)) Continue; Builder. Append (type.to string () + ":"); Manufacturer (app) (card where (f => f. card type == type) builder.Append (Environment.NewLine); foreach (cards.Where (f = & gt; CardViewModel card; f.CardType == type)) {builder.Append (card.AmountInDeck.ToString () + "\ t" + card.NameName + Environment.NewLine);} Builder .Append (EnvironmentalNewLine); }} And if (as above, the same code block is one of the cardview models. The property type T is always an enum for sorting the "card"   

for the property looking up. So far, I have the following method of calling:

 Private Zero AddSections & lt; T & gt; (IEnumerable & lt; CardViewModel & gt; Cards, StringBuilder Builder, & lt; CardViewModel, T & OR; Order of the ceremony & lt; CardViewModel, bool & Gt; predicate) {value of cards in cards = cards.OrderBy (order); foreach (Enum.GetValues ​​(typeof (T)) (if (! Card anyone) ()); Builder.Padend (value.ToString (+): "); Builder.Append (cards.Where (predicate) .sum (f = & gt; f.AmountInDeck)); builder.Append (Environment.NewLine); Foreach (CardViewModel card cards.Where (in predicate)) {builder. Add (card.mount inkock.Toastring () + "\ t" + card .name + environment + new line);} Builder. Appad (environment. New);}}   

However, now I have a problem that AddSections to be filtered, because I need a different meaning for each part of the outer loop. I can not pass it as a fixed parameter because I can not explicitly reference "value" in the external foreach loop. So I think that I have to create it dynamically inside the AddSections function. If this is the case, how can I do this or am completely wrong?

move here to Func & lt; CardViewModel, bool & gt; Change is with Predicate with function & lt; T, fx & lt; Cardwydmodel, boolean & gt; & Gt; . PredicateFactory

I think this works:

  Private Zero AddSections & LT; T & gt; (IEnumerable & lt; CardViewModel & gt; Card, StringBuilder Builder, & lt; CardViewModel, T & gt; Order, Funk & lt; t, Funk & lt; CardviewModel, Balls & gt; predicateFactory) of the ceremony {cards = cards. OrderBy (order); Foreign currency (T-value (Typef (T)) in Enum.GetValues ​​(if issued (card; any predicateFactory (value)); Builder .Append (value.tostring) + ":"); Builder.Append (Environment.NewLine); Foreach (Card in CardModel Card Card) where (predicateFactory (value) ))} {builder.Append (card.AmountInDeck.ToString () + "\ t" + card.NameName + Environment.NewLine);} Builder .Append (EnvironmentalNewLine);}}   

AddSections :

  this.AddSections  (Car D, Builder, M => M Card Type, V => M => M Card Type == V);   

Alternatively , You may be able to shortcut the need to pass only one ingenious (or ingenious factory) at all. It seems that you are just working with enums since you can do this:

 <0> Private Zero AddSections & lt; t & gt; (IEnumerable & lt; CardViewModel & gt; Card, StringBuilder Builder, & lt; CardViewModel, T & gt; Order) {card = card. Order buy (order); Foreign currency (T-value (Typef (T)) in Enum.GetValues ​​(if (Cards! Any (c => Order (c) == value); Builder .Append (Value.tostring ) ":"); Manufacturer (app) (card where (c => order (c) == value) .Sum (f = & gt; f.AmountInDeck); builder.Append (Environment.NewLine ); foreach (cards.Where CardViewModel Card (c = & gt; Order (c) == Price)) {builder.Append (card.AmountInDeck.ToString () + "\ t" + card.NameName + Environment .NewLine);} Builder .Append (EnvironmentalNewLine);}}   

And then your call is done:

  this.AddSections & lt; CardTypes & gt; (Card, Builder, M = & gt; m.CardType);    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -