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 ?
Related
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);
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))
});
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
I have a list something like that
var x = new List<string>{"A","B","C","D","E","F"};
I want to group it in doubles like that
1-A
1-B
2-C
2-D
3-E
3-F
What my solution is
var result = new Dictionary<int, List<string>>();
var sequence = 1;
var groupList = new List<string>();
for (var i = 0; i < x.Length ; i++)
{
groupList.Add(x[i]);
if ((i + 1)%2 == 0)
{
result.Add(sequence, groupList);
groupList = new List<string>();
sequence++;
}
}
return result;
I am looking for alternative (better) ways to get same result.
Here is another way:
int numPairs = 3;
char[] abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
var dict = new Dictionary<int, char[]>();
for (int i = 1, k=1; i <= numPairs && k<abc.Length; i++, k+=2)
{
dict.Add(i, new char[] { abc[k - 1], abc[k] });
}
return dict;
This will do the same.
var y = x.GroupBy(i =>Convert.ToInt32((x.IndexOf(i) + 1.1) * 0.5));
I think this is a readable Linq solution:
var result = x
.Select((value, index) => new { value, index })
.GroupBy(arg => arg.index / 2 + 1, arg => arg.value);
The Select projects the value with its index into an anonymous type, and the GroupBy groups by the index.
If you need the groupings in a Dictionary like in your question, use ToDictionary:
var dictionary = result.ToDictionary(
grouping => grouping.Key,
grouping => grouping.ToList());
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Model.GetData.Where(p => p.Value != null).ToList();
I have values in my database like below,
My Value Column:
Null
Null
5
16
ExampleValue1
ExampleValue2
p.value is string
I only want to see "ExampleValue1" and "ExampleValue2".
How can i get datas not null and not numeric ?
Perhaps:
var BillingNumbers = Model.GetData
.Where(p => p.Value != null && SqlFunctions.IsNumeric(p.Value) == 0);
Assuming the results are of different types, you can query by type like this:
var results = from item in collection where (item is String) select item;
Example program:
static void Main(string[] args)
{
var collection = new List<Object>
{
5,
1.5,
null,
"foo",
"bar",
};
var results = from item in collection where (item is String) select item;
foreach (var result in results)
{
Console.WriteLine(result);
}
System.Threading.Thread.Sleep(10000);
}
You can rely to this code :
result is just contains string
List<object> result = new List<object>();
var MyList = new List<Object>{
523,
2.555,
null,
"ExampleValue1",
"ExampleValue2",
};
MyList.ForEach(x =>
{
if (x != null)
{
if (!string.IsNullOrEmpty(Regex.Replace(x.ToString(), #"^[0-9]*(?:\.[0-9]*)?$", string.Empty)))
{
result.Add(x);
}
}
});
Try on this..
int i;
var resultList = Model.GetData.Where(p => p.Value != null).ToList();
var finalResult= resultList.Where( t => !int.TryParse( t.Value , out i));
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
for each number in A and each number in B if their difference is more than 3 ,add the new number
A={6,7,10}
B={2,3}
result={4,5,4,8,7}
for example :
6-2=4 true Add 4
6-3=3 false
7-2=5 true Add 5
my attempt :
var result = A.Select((a, i) => new
{
desired = a - B[i] > 3 ? a - B[i] :0
});
It sounds like you want:
var query = from a in A
from b in B
let diff = a - b
where diff > 3
select diff;
Or in dot notation, which drops out really neatly:
var query = A.SelectMany(a => B, (a, b) => a - b)
.Where(x => x > 3);
With your test data:
using System;
using System.Linq;
class Test
{
public static void Main()
{
int[] A = {6, 7, 10};
int[] B = {2, 3};
var query = A.SelectMany(a => B, (a, b) => a - b)
.Where(x => x > 3);
foreach (var result in query)
{
Console.WriteLine(result);
}
}
}
var query = from a in A
from b in B
where (a - b) > 3
select a - b;