I have the following string;
string[,] test=
{
{"0","0"},
{"0","0"},
{"0","0"},
{"0","1"},
{"5","0"},
};
I'd like to find a way to search the array for all lines that are unique.
So my desired output array would be.
string[,] output=
{
{"0","0"},
{"0","1"},
{"5","0"},
};
Does anyone have any simple ideas on how to achieve this?
you can do this with linq just call Distinct to select only unique values, like this
var query = (from arr in test
from value in arr
select value).Distinct();
or you can try the following method, This simply flattens the nested arrays into a sequence and calls Distinct to find the distinct elements. source this answer
var distinct = test.SelectMany(a => a).Distinct().ToArray();
Related
Ok what i want is simple but i could not write it properly
I want to replace each value of a string list with multiple values (another list hold this values). I can write several loops however i want to execute it with a single line of command by using linq
lstCrawlUrls is a List<string>
lstReplaceWordsFromUrls is a List<KeyValuePair<string,string>>
The below is not working as i am making a syntax error i need help to fix it thank you
lstCrawlUrls = lstCrawlUrls.Select(pr =>lstReplaceWordsFromUrls
.ForEach{mr =>( pr.Replace(mr.Key, mr.Value); }))
.ToList<string>();
c# .net 4.5
simple example
ListA (string list) = "home","work","play","swim"
listB (keyvalue pairs) = "me;aa","or;cc"
I replace values of ListA with listB so the listA becomes as below
"hoaa","wcck","play","swim"
If you insist on a one-liner, here's how to do it:
lstCrawlUrls = lstCrawlUrls.Select(pr => lstReplaceWordsFromUrls.Aggregate(pr, (str, mr) =>( str.Replace(mr.Key, mr.Value); )));
I'm actually using a list which contains lists which contains lists (yes three sets):
List<List<List<int>>> threeD = new List<List<List<int>>>();
after I fill this monster, I get this:
var threeD = [ [[18,100],[13,95]], [[12,100],[2,5],[7,45]], [[19,100]] ];
The above list contains three separate lists with a list with an x and y int. How can I sort these based on the first number. My output I would need would be this:
var threeD = [ [[13,95],[18,100]], [[2,5],[7,45],[12,100]], [[19,100]] ];
I was thinking of maybe sorting using some kind of linq statement. Or should I maybe proceed by creating nested for loops?
Edit:
I only need to sort the inner-most lists and not the outer lists. They are actually cordinates for a line graph with multiple lines using jqplot so the order that each line is rendered doesn't really matter. Thanks
You first sort the inner list of x/y numbers, and then you order the outer list:
threeD.Select(sublist => sublist.OrderBy(xy => xy[0]).ToList())
.OrderBy(sublist => sublist[0][0])
.ToList();
And if you just need to sort the inner list, just leave the outer sort out:
threeD.Select(sublist => sublist.OrderBy(xy => xy[0]).ToList()).ToList();
I am going to ask a very basic question and probably a repeated one but I have a bit different situation.
I want to use "in" operator in Linq.
I have to get all the rows from table which has Id provided
by my array and returns the row if it has. How can I do it.
My array has
var aa="1091","1092","1093" and so on.
and my table uses these Ids as Primary keys
.I have to get all the rows whose Id is contained in the array and I do not want to use S.P.
You can use Enumerable.Contains,
var aa = new string[3] { "1091", "1092", "1093" };
var res = yourDataSource.Where(c => aa.Contains(c.ID));
IN statements are created by using Contains in your Where call. Assuming you use integers as IDs, you could write something like this:
var myArray=new[]{1091,1092,1094};
var myEntities=from entity in myTable
where myArray.Contains(entity.ID)
select entity;
I have 3 list List
ListMaster contains {1,2,3,4,....} ..getting populated from DB
List1 contains {1,3,4}
List2 contains {1,3,95}
how to check Which list items are present in master list using linq
var inMaster = List1.Intersect(ListMaster);
or for both list :
var inMaster = List1.Intersect(List2).Intersect(ListMaster);
check if any item from list1, list2 exist in master
var existInMaster = inMaster.Any();
You can use Enumerable.Intersect:
var inMaster = ListMaster.Intersect(List1.Concat(List2));
If you want to know which are in List1 which are not in the master-list, use Except:
var newInList1 = List1.Except(ListMaster);
and for List2:
var newInList2 = List2.Except(ListMaster);
Can i use a list .all to check all item of a list in another list for
list of string
So you want to know if all items of one list are in another list. Then using Except + Any is much more efficient(if the lists are large) because Intersect and Except are using sets internally whereas All loops all elements.
So for example, does the master-list contain all strings of List1 and List2?
bool allInMaster = !List1.Concat(List2).Except(ListMaster).Any();
You can use Enumerable.Intersect method like;
Produces the set intersection of two sequences by using the default
equality comparer to compare values.
var inMaster1 = List1.Intersect(ListMaster);
var inMaster2 = List2.Intersect(ListMaster);
Here is a DEMO.
I have a list of array of string: List<string[]> myList;
Is there any way to get an array of strings with all the elements string[0]?
Like List myList to string[] where elements are myList[string[0]]
I suppose it´s something like var result = from x in myList .Take(0) select x
And question 2)
is there any way to convert a string[] to string[1,] without a for loop?
Now I do:
for (int i = 0; i < arr.Length; i++)
range[0, i] = arr[i];
I need "this syntax" because I´m exporting columns to excel. And I need an object[,] to do a range.set_Value(Type, object[,]).
string[] result = myList.Select(arr => arr.FirstOrDefault()).ToArray();
Is that what you're looking for?
Take(n) only returns an enumerable with at most n elements, so that's obviously wrong.
From what I gather from your question, this should suffice:
string[] result = (from item in myList select item.FirstOrDefault()).ToArray());
For the first part;
var temp = myList.Select(q => q.FirstOrDefault());
For the second part;
I do not think there is a direct Linq statement to convert a single dimension array to a multi dimension one.