From: Aeden Jameson on
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
From: Andrey on
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 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 am struggling with how one would write a linq expression to select
all objects that have the lowest cost. There is no guarantee of
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/tutorials/aspnet/f6fc20ab-5c6a-4f04-8a0b-bba39e4bbcf0/biztalk-incorporating-co.aspx
From: Andrey on
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 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 am struggling with how one would write a linq expression to select
all objects that have the lowest cost. There is no guarantee of
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/tutorials/aspnet/c3ac8915-3861-4406-bec7-42f2d9110d79/break-the-roles-in-sharep.aspx
From: Peter Duniho on
Aeden Jameson wrote:
> 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