Linq Expression for All Objects With Lowest Cost

A

Aeden Jameson

Suppose I have the class foo

class Foo
{
PropertyOne;
PropertyTwo;
}

and I have a cost calculator class that uses lots of different
information to
calculate a cost.

class CostCalculator
{
public decimal Calculate(Foo foo);
}


I'm struggling with how one would write a linq expression to select
all objects that have the lowest cost. There's no guarantee of
uniqueness. Thanks for your help.

Cheers,
Aeden
 
A

Andrey Dudarev

First you need create collection or IEnumerable of Foo objects:

List<Foo> Foos = GetFoos(....);

,then

CostCalculator calculator = new CostCalculator ();
var minCost = Foos.Min( s=> calculator(s)).FirstOrDefault();




Aeden Jameson wrote:

Linq Expression for All Objects With Lowest Cost
12-Apr-10

Suppose I have the class fo

class Fo

PropertyOne
PropertyTwo


and I have a cost calculator class that uses lots of differen
information t
calculate a cost

class CostCalculato

public decimal Calculate(Foo foo)


I am struggling with how one would write a linq expression to selec
all objects that have the lowest cost. There is no guarantee o
uniqueness. Thanks for your help

Cheers
Aeden

Previous Posts In This Thread:


Submitted via EggHeadCafe - Software Developer Portal of Choice
BizTalk: Incorporating conditional If / Else Functoid Logic in a map.
http://www.eggheadcafe.com/tutorial...0b-bba39e4bbcf0/biztalk-incorporating-co.aspx
 
A

Andrey Dudarev

List<Foo> foos = ... method to create a collection of Foos...

CostCalculator calc = new CostCalculator();
IEnumerable<Foo> minCosts = foos.Min( s=> calc(s));



Aeden Jameson wrote:

Linq Expression for All Objects With Lowest Cost
12-Apr-10

Suppose I have the class fo

class Fo

PropertyOne
PropertyTwo


and I have a cost calculator class that uses lots of differen
information t
calculate a cost

class CostCalculato

public decimal Calculate(Foo foo)


I am struggling with how one would write a linq expression to selec
all objects that have the lowest cost. There is no guarantee o
uniqueness. Thanks for your help

Cheers
Aeden

Previous Posts In This Thread:


Submitted via EggHeadCafe - Software Developer Portal of Choice
Break the Roles in SharePoint Lists
http://www.eggheadcafe.com/tutorial...7-42f2d9110d79/break-the-roles-in-sharep.aspx
 
P

Peter Duniho

Aeden said:
Suppose I have the class foo

class Foo
{
PropertyOne;
PropertyTwo;
}

and I have a cost calculator class that uses lots of different
information to
calculate a cost.

class CostCalculator
{
public decimal Calculate(Foo foo);
}


I'm struggling with how one would write a linq expression to select
all objects that have the lowest cost. There's no guarantee of
uniqueness. Thanks for your help.

I don't know that there would be code that is expressible solely as a
LINQ expression and which would do that. However, you can certainly
take advantage of LINQ to do something like that.

For example:

IEnumerable<Foo> data = …;
CostCalculator calc = …;

var ordered = from foo in data
select new { Foo = foo, Cost = calc.Calculate(foo) }
orderby Cost;

decimal minCost = ordered.First().Cost;

var least = from fooAndCost in ordered
where fooAndCost.Cost == minCost
select fooAndCost.Foo;

There are other ways you might accomplish the same thing using LINQ, but
the above should work and isn't too inefficient. :)

If the above doesn't address your question, you should try to be more
specific.

Pete
 
Joined
Apr 16, 2010
Messages
3
Reaction score
0
yes you are near... here is how it should work


List<Foo> foos = ... method to create a collection of Foos...

CostCalculator calc = new CostCalculator();
IEnumerable<Foo> minCosts = foos.Min( s=> calc.Calculate(s));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top