Convert to LINQ lambda expression 4 [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how to convert to lambda:
var source = new Item[]{
new Item { Key = "A", ID = 1, Text = "Hello" },
new Item { Key = "A", ID = 2, Text = "World" },
new Item { Key = "B", ID = 2, Text = "Bar" },
new Item { Key = "B", ID = 1, Text = "Foo" }
};
var results = (from r in source
group r by r.Key.ToString() into g
select new
{
g.Key,
Data = string.Join("", g.OrderBy(s => s.ID).Select(s => s.Text))
});
It is possible to convert?
Thanks for answer

How about this?
var results = source.GroupBy(r => r.Key).Select(g => new
{
g.Key,
Data = string.Join("", g.OrderBy(s => s.ID).Select(s => s.Text))
});

Related

Finding the smallest closest value in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have the code as below
var searchValues = new double[] { 21.1, 21.65, 22.2, 22.75, 23.3, 23.85, 24.4, 24.95, 25.5, 26.05, 26.6, 27.15, 27.7, 28.25, 28.8, 29.35, 29.9, 30.45, 31, 31.55, 32.1, 32.65, 33.2, 33.75, 34.3, 34.85, 35.4, 35.95 };
var searchValue = 22;
double nearest = searchValues .Select(p => new { Value = p, Difference = Math.Abs(p - searchValue ) })
.OrderBy(p => p.Difference)
.First().Value;
This code returns me 22.2 . However i want the result to be the smallest value close to 22, i.e it will be 21.65. How do i achieve this? I am relatively new to C# and would appreciate some help.
As per your expected output you are trying to find closed value which is less than searchValue. Try below
var searchValues = new double[] { 21.1, 21.65, 22.2, 22.75, 23.3, 23.85, 24.4, 24.95, 25.5, 26.05, 26.6, 27.15, 27.7, 28.25, 28.8, 29.35, 29.9, 30.45, 31, 31.55, 32.1, 32.65, 33.2, 33.75, 34.3, 34.85, 35.4, 35.95 };
var searchValue = 22;
double nearest = searchValues
.Where(x => x <= searchValue) //Filter all values which are less than or equal to searchValue
.OrderBy(y=> y) //Order resultant values by ascending order
.LastOrDefault(); //Get the max value from an array.
Console.WriteLine(nearest == 0 && searchValue > 0 ? "Invalid Input" : $"Nearest Value : {nearest}");
.net Fiddle
You can just apply Math.Round() in difference.
var searchValues = new double[] { 21.1, 21.65, 22.2, 22.75, 23.3,
23.85, 24.4, 24.95, 25.5, 26.05, 26.6, 27.15, 27.7,
28.25, 28.8, 29.35, 29.9, 30.45, 31, 31.55, 32.1,
32.65, 33.2, 33.75, 34.3, 34.85, 35.4, 35.95 };
var searchValue = 22;
double nearest = searchValues.Select(p => new { Value = p, Difference
= Math.Round(Math.Abs(p - searchValue)) })
.OrderBy(p => p.Difference)
.First().Value;
Console.WriteLine(nearest);

LINQ inner WHERE - inner collection not contains string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Suppose I have the following collection:
ONE
- Banana
- Mango
TWO
- Apple
- Mango
THREE
- Orange
- Pear
I want to get only the collection which do not have Mango in it, such as:
THREE
- Orange
- Pear
The following example returns wrong result:
List<Order> list = new List<Order> {
new Order { Id = 1, Name = "ONE", Items = new List<Items> { new Item { Id = 1, Nama = "Banana" }, new Items { Id = 2, Nama = "Mango" } }},
new Order { Id = 1, Name = "TWO", Items = new List<Items> { new Item { Id = 1, Nama = "Orange" }, new Items { Id = 2, Nama = "Mango" } }},
new Order { Id = 1, Name = "THREE", Items = new List<Items> { new Item { Id = 1, Nama = "Pear" }, new Items { Id = 2, Nama = "Chery" } }},
};
var result = list.Where(x => x.Item.Any(y => y.Nama != "Mango")).ToList();
Any will bail out as soon as a "Nama" different from 'Mango' is found which is not what you want.
x.Items.All(y => y.Nama != "Mango")
Should do it.

How to extract data efficiently? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
There is the next data structure:
var parents = new List<Parent>
{
new Parent
{
Children = new List<Child>
{
new Child { Name = "Name1", Value = 100 },
new Child { Name = "Name2", Value = 0 },
}
},
new Parent
{
Children = new List<Child>
{
new Child { Name = "Name1", Value = 0 },
new Child { Name = "Name2", Value = 200 },
}
},
new Parent
{
Children = new List<Child>
{
new Child { Name = "Name1", Value = 0 },
new Child { Name = "Name2", Value = 200 },
}
},
new Parent
{
Children = new List<Child>
{
new Child { Name = "Name1", Value = 100 },
new Child { Name = "Name2", Value = 0 },
}
}
};
Parents have the similar children but its values can be binary different (0 or some value).
The output should be:
var output = new List<Child>
{
new Child { Name = "Name1", Value = 100 },
new Child { Name = "Name2", Value = 200 },
};
Is there efficient and compact way to retrieve data in this way via LINQ or extension methods?
Most of the answers are very close but they have small imperfections, in my humble opinion. Here's how I would write it:
var output = parents
.SelectMany(p => p.Children)
.Where(c => c.Value != 0)
.GroupBy(c => c.Name)
.Select(g => g.First())
.ToList();
Maybe I'm misunderstanding but can't you just use SelectMany ?
parents.SelectMany(x=>x.Children).Where(x=>x.Value != 0).Distinct().ToList();
edit: Added .Distinct() per #GSerg's comment
This avoids custom Equality operator or copies of your objects:
parents.SelectMany(p => p.Children).GroupBy(c => c.Name).Select(g => g.FirstOrDefault(c
=> c.Value != 0)).ToList().

LINQ - double grouping with elements counting [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
As example I have the following list of PlanesLogRow objects
public class PlanesLogRow
{
public DateTime ArriveDate;
public string Origin;
public string Destination;
}
Need to get list of all of airports (Origin + Destination).Distinct()
Need to calc for every airport "arrived to airport" and "left from the airport" counts.
I need to create by one LINQ string tuple like <airport, AsOriginCount(arrivedToCount), AsDestinationCount(LeftFromCount)>
To get list of all of airports isn't a problem, but not sure how this can be done in case of such double grouping by different parameters
If you have list of planes you can project each plane object into two anonymous objects - one for destination, and one for origin. Then group these anonymous objects by airport and calculate totals:
planes.SelectMany(p => new[] {
new { Airport = p.origin, IsOrigin = true },
new { Airport = p.destination, IsOrigin = false }
})
.GroupBy(x => x.Airport)
.Select(g => new {
Airport = g.Key,
AsOriginCount = g.Count(x => x.IsOrigin),
AsDestinationCount = g.Count(x => !x.IsOrigin)
})
For given planes:
var planes = new List<Plane> {
new Plane { Origin = "Minsk", Destination = "London" },
new Plane { Origin = "Barcelona", Destination = "Minsk" },
new Plane { Origin = "Rome", Destination = "Minsk" },
new Plane { Origin = "Barcelona", Destination = "London" },
new Plane { Origin = "London", Destination = "Rome" },
};
Output will be:
[
{ "Airport": "Minsk", "AsOriginCount": 1, "AsDestinationCount": 2 },
{ "Airport": "London", "AsOriginCount": 1, "AsDestinationCount": 2 },
{ "Airport": "Barcelona", "AsOriginCount": 2, "AsDestinationCount": 0 },
{ "Airport": "Rome", "AsOriginCount": 1, "AsDestinationCount": 1 }
]
Update: This query will work with Entity Framework. Generated SQL will be big and scary.

Compare to strings and get the distinct in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Example :
string a= "6,9";
string b= "5,9";
on comparing the above i need unique elements
result:
a=6;
b=5;
string a= "6,9"; string b= "5,9";
string uniquea = string.Join(",", a.Split(',').Except(b.Split(',')));
string uniqueb = string.Join(",", b.Split(',').Except(a.Split(',')));
This will keep duplicates within a and b.
string a= "6,9"; string b= "5,9";
char[] splitters = new[] { ',', ' '};
var aList = a.Split(splitters);
var bList = b.Split(splitters);
var uniqueA = aList.Except(bList).ToList();
var uniqueB = bList.Except(aList).ToList();
string[] assets, allassets = null;
int[] list1, list2, removed_list, added_list = null;
assets = a.Split(',');
list1 = Array.ConvertAll(assets, x => int.Parse(x))
allassets = b.Split(',');
list2 = Array.ConvertAll(allassets, x => int.Parse(x));
removed_list = list2.Where(x => !list1.Contains(x)).ToArray(); // which gives =>a
added_list = list1.Where(x => !list2.Contains(x)).ToArray(); // which gives =>b
I am done with this ,What u people say ?

Categories