I have a BaseVals series acquired from sql by a linq query on my chart. I need to put tolerance boundaries on the chart also depending on this BaseVals series' Y points.
X points of these boundary series will remain same. I am using below code for querying
int max1 = dataContext.SupplierVals
.Where(m => m.MouldID == mouldId && m.Plane == plane)
.Max(m => m.MeasId);
var query = dataContext.SupplierVals
.Where(m => m.MouldID == mouldId
&& m.MeasId == max1).ToList();
if (cbBenchType.SelectedIndex == 0)
{chartBench.Series["BaseVals"].Points.DataBind(query, "Distnc", "Apert", null);
}
How can i add 2 series (y points of the series "BaseVals" + 0.1) and
(y points of the series "BaseVals" - 0.1)?
Related
want to Pivot this table using linq c#
My Table is here
Since the question does not provide what to pivot by.. I did a pivot to count on a period of 50. Change it to your preference. Check this fiddle.
var result = myList
.GroupBy(x => x.Branch)
.Select(y => new {
Branch = y.Key,
FirstPeriod = y.Count(z => z.Quantity > 100 && z.Quantity <= 150),
SecondPeriod = y.Count(z => z.Quantity > 150 && z.Quantity <= 200),
ThirdPeriod = y.Count(z => z.Quantity > 200 && z.Quantity <= 250)
}).ToList();
References:
Excellent Pivot Example
Method used in the fiddle
I have a two dimensional array containing objects of type MyObj.
private MyObj[,] myObjs = new MyObj[maxX, maxY];
I want to get the indices from the array when passing in a matching object. I want to get the x and y value from this array. I can return these two values as a Position object that takes a x and y coordinate.
private Position GetIndices(MyObj obj)
{
for (int x = 0; x < myObjs.GetLength(0); x++)
{
for (int y = 0; y < myObjs.GetLength(1); y++)
{
if (myObjs[x, y] == obj)
{
return new Position(x, y);
}
}
}
}
Is it possible to get this code shorten to some Linq code lines?
But I don't think, it looks nice :)
var result = Enumerable.Range(0, myObjs.GetLength(0))
.Select(x => Enumerable.Range(0, myObjs.GetLength(1)).Select(y => new { x, y }))
.SelectMany(o => o)
.FirstOrDefault(o => myObjs[o.x, o.y] == obj);
Here's another option, if you're interested. It uses an indexer inside the first select and does a little math to find where that index falls inside the two-dimensional array.
var o = new MyObj();
myObjs[1,2] = o;
var p = myObjs.Cast<MyObj>()
.Select((x,i) => Tuple.Create(x,i))
.Where(x => x.Item1 == o)
.Select(x => new Point(x.Item2 / myObjs.GetLength(1), x.Item2 % myObjs.GetLength(1)))
.SingleOrDefault();
Console.WriteLine(p); // prints {X=1,Y=2}
It sorta looks like you're considering the x-coordinate to be the height of the array, and the y-coordinate the width, in which case you'd want to switch it up slightly:
var p = myObjs.Cast<MyObj>()
.Select((x,i) => Tuple.Create(x,i))
.Where(x => x.Item1 == o)
.Select(x => new Point(x.Item2 % myObjs.GetLength(1), x.Item2 / myObjs.GetLength(1)))
.SingleOrDefault();
Console.WriteLine(p); // prints {X=2,Y=1}
I used Point instead of Position since it's built into .NET but you should be able to just swap one for the other.
Yes, that is possible. You can ask Resharper to do the job (loop to linq) for you. After installation, just use the feature.
I'm trying to query a nested linked list where result is the product of a child value multiplied by values in the parent list, and then the result is added.
This code works:
var X = (from B in Main.Globals.BookLL
from G in B.GreeksLL
where B.DealNo == 1 && B.Strategy == "Condor" &&
G.BookOrComp == "Book" && G.GreekType == "Delta"
select new
{
Total = G.Data[3] * B.BookPosn * B.FxRate
}).Sum(s=>s.Total);
...but I'd prefer to use lambda. This code below gives the compile error shown as a comment at the end of the line.
double Z = Globals.BookLL.Where(B => B.DealNo == 1 && B.Strategy == "Condor").
SelectMany(G => G.GreeksLL).
Where(G => G.BookOrComp == "Book" && G.GreekType == "Delta").
Select(G => new { Total = G.Data[3] * B.BookPosn*B.FxRate }). // Compile error "B does not exist in the current context"
Sum();
I don't know how to do this, please take a look and correct the query? Thanks.
Try:
double Z = Globals.BookLL.Where(B => B.DealNo == 1 && B.Strategy == "Condor").
SelectMany(par => par.GreeksLL, (parent, child) => new { G = child, B = parent }).
Where(both => both.G.BookOrComp == "Book" && both.G.GreekType == "Delta").
Select(both => new { Total = both.G.Data[3] * both.B.BookPosn*both.B.FxRate }).
Sum(x => x.Total);
My naming is a bit weird, but I hope you get the idea, basically you 'abandoned' B when you did SelectMany(), and this should be the way.
It is untested, so let me know if it works.
See MSDN for SelectMany() with results selector function.
Another approach is to filter the GreekLL inside the SelectMany using this overload, and then use Sum extension:
double z = Main.Globals.BookLL.Where(book => book.DealNo == 1 && book.Strategy == "Condor")
.SelectMany(book => book.GreeksLL.Where(greek => greek.BookOrComp == "Book" && greek.GreekType == "Delta")
,(book, greek) => new { Greek = greek, Book = book })
.Sum(greekAndBook => greekAndBook.Book.BookPosn * greekAndBook.Book.Fxrate * greekAndBook.Greek.Data[3]);
I have a list of Points. List<Point> points = new List<Point>();
I want to get the biggest value of y coordinates inside the list, given the condition that it only scans the coordinates inside the list with a specific x coordinate.
For example:
I have points like this
(1,1)
(1,2)
(1,3)
(1,4)
(1,5)
(2,1)
(2,2)
(2,3)
(2,4)
(2,5)
I want to find the biggest value of y-axis or y-coordinate, given that it only search coordinates with 2 as value of x. so the output must be (2,5)
Use LINQ to get point with biggest Y coordinate:
Point maximumPoint = points.First(p => p.X == 2 &&
p.Y == points.Max(po => po.Y));
OR
Point maximumPoint = new Point(2, points.Where(p => p.X == 2).Max(p => p.Y));
With LINQ, you can do:
var result = points.Where(point => point.X == 2)
.Max(point => point.Y);
If you want the Point instead of just the Y-coordinate, it's simple enough to just new up a Point wih (2, result).
But in more complex cases, consider a MaxBy operator, such as the one that comes with morelinq.
With linq, this would by
var maxY = points.Where(p => p.X == 2).Select(p=> p.Y).Max();
This perhaps:
var maxY = points.Where(po => po.X == 2).Max(po => po.Y);
var q = points.Where(p =>
p.Y == maxY &&
p.X == 2).FirstOrDefault();
I have a problem. I'm finding an intersection using the code below:
Envelope[][] extents = new Envelope[tilesCountX][tilesCountY];
// some code here to set "extents" values
var intersectedTiles =
extents
.SelectMany(es => es)
.Where(e => EnvIntersects(e, bounds))
.ToList();
private static bool EnvIntersects(Envelope e1, Envelope e2)
{
return e1.MinX >= e2.MinX && e1.MaxX <= e2.MaxX && e1.MinY >= e2.MinY && e1.MaxY <= e2.MaxY;
}
It works but I want to get the indexes of intersected extents.
e.g.
If extents[2][7] is an intersected element, I want to get 2 and 7.
Is it possible by modifying my code?
[edit]
bounds is an Envelope that has MinX, MinY, MaxX and MaxY properties inside.
Envelope bounds = new Envelope();
bounds.MinX = some_value_1;
bounds.MaxX = some_value_2;
bounds.MinY = some_value_3;
bounds.MaxY = some_value_4;
I think this could also give you what you want:
var intersectedIndices =
from x in Enumerable.Range(0, tilesCountX)
from y in Enumerable.Range(0, tilesCountY)
where EnvIntersects(extents[x, y], bounds)
select new { x, y };
Try this:
var intersectedTiles =
extents
.SelectMany((es,i) => es.Select((x,j)=>new{I=i,J=j,Element = x}))
.Where(e => EnvIntersects(e.Element, bounds))
.ToList();
where for all elements in intersectedTiles this is true:
intersectedTiles[x].Element == extents[intersectedTiles[x].I][intersectedTiles[x].J]