 We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

# Unable to write Calculate using Linq[Answered] RSS

## 4 replies

Last post Mar 30, 2021 07:50 AM by shabbir_215

• ### Unable to write Calculate using Linq

Mar 29, 2021 09:57 PM|shabbir_215|LINK

Hi All,

Please can you help to calculate below request using LINQ

1. data structure to represent a customer, with the following fields. The coordinates represent a grid location on a 100 x 100 map, with all the units being kilometres.
• Name (the customer’s name)
• X (the X grid coordinate on a map as an integer value)
• Y (the Y grid coordinate on a map as an integer value)
2.  Customer Name X Y Customer 1 10 20 Customer 2 90 24 Customer 3 34 63 Customer 4 67 1 Customer 5 24 84 Customer 6 51 44 Customer 7 97 92 Customer 8 77 13 Customer 9 35 39 Customer 10 85 29

Write a function that takes two input parameters, the starting X and Y coordinates. The function should calculate the shortest route that it takes to visit all ten customers, starting at the initial coordinates passed in to the function. Any distance calculations used in your algorithm should ignore fractional values and integer arithmetic should be used throughout

Many Thanks,

SHabbir

• ### Re: Unable to write Calculate using Linq

Mar 29, 2021 10:08 PM|mgebhard|LINK

Come on man, really?   It sounds like homework or an interview question.  Either way, I'm sure the entity that is asking you to solve this problem wants you're work not someone on the Internet.

• ### Re: Unable to write Calculate using Linq

Mar 30, 2021 06:02 AM|shabbir_215|LINK

#### mgebhard

Come on man, really?   It sounds like homework or an interview question.  Either way, I'm sure the entity that is asking you to solve this problem wants you're work not someone on the Internet.

thanks for your kind Advice, I had already tried so thought of sharing to all so they might have better solution.

but that’s was really rude reply

• ### Re: Unable to write Calculate using Linq

Mar 30, 2021 06:57 AM|yij sun|LINK

Hi shabbir_215,

I have created a demo that you could get the customer's name which have the shortest route.Just like this:

```static void Main(string[] args)
{
List<P> pxy = new List<P>
{
new P{ Name="Customer1", X=10,Y=20},
new P{ Name="Customer2",X=90,Y=24},
new P{ Name="Customer3",X=34,Y=63},
new P{ Name="Customer4",X=67,Y=1},
new P{ Name="Customer5",X=24,Y=84},
new P{ Name="Customer6",X=51,Y=44},
new P{ Name="Customer7",X=97,Y=92},
new P{ Name="Customer8",X=77,Y=13},
new P{ Name="Customer9",X=35,Y=39},
new P{ Name="Customer10",X=85,Y=29}
};
Console.WriteLine("Please enter X");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter Y");
int y = int.Parse(Console.ReadLine());

var closestToOrigin = pxy
.Select(p => new { P = p, Distance2 = Math.Sqrt((x-p.X)*(x-p.X)+(y-p.Y)*(y-p.Y)) })
.Aggregate((p1, p2) => p1.Distance2 < p2.Distance2 ? p1 : p2)
.P;

}```

Best regards,

Yijing Sun

ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
• ### Re: Unable to write Calculate using Linq

Mar 30, 2021 07:50 AM|shabbir_215|LINK

#### yij sun

Hi shabbir_215,

I have created a demo that you could get the customer's name which have the shortest route.Just like this:

```static void Main(string[] args)
{
List<P> pxy = new List<P>
{
new P{ Name="Customer1", X=10,Y=20},
new P{ Name="Customer2",X=90,Y=24},
new P{ Name="Customer3",X=34,Y=63},
new P{ Name="Customer4",X=67,Y=1},
new P{ Name="Customer5",X=24,Y=84},
new P{ Name="Customer6",X=51,Y=44},
new P{ Name="Customer7",X=97,Y=92},
new P{ Name="Customer8",X=77,Y=13},
new P{ Name="Customer9",X=35,Y=39},
new P{ Name="Customer10",X=85,Y=29}
};
Console.WriteLine("Please enter X");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter Y");
int y = int.Parse(Console.ReadLine());

var closestToOrigin = pxy
.Select(p => new { P = p, Distance2 = Math.Sqrt((x-p.X)*(x-p.X)+(y-p.Y)*(y-p.Y)) })
.Aggregate((p1, p2) => p1.Distance2 < p2.Distance2 ? p1 : p2)
.P;

}```

Best regards,

Yijing Sun

Thank You so much Yijing Sun, it worked perfect.

I owe you a cup of coffee

Thanks Again