I have list of int A,B. i like to do the following step in linq
list<int> c = new List<int>();
for (int i = 0; i < a.count; i++)
{
for (int j = 0; j < b.count; j++)
{
if (a[i] == b[j])
{
c.add(a[i]);
}
}
}
if its a and b is object , I need check particular properties like this manner and add list if it equals how can i do this in linq?
You could use the Intersect method:
var c = a.Intersect(b);
This return all values both in a and b. However, position of the item in the list isn't taken into account.
You can use Intersect:
var a = new List<int>();
var b = new List<int>();
var c = a.Intersect(b);
Produce a list c containing all elements that are present in both lists a and b:
List<int> c = a.Intersect(b).ToList();
The LINQ equivalent of your code is:
var c = from i in Enumerable.Range(0, a.Count)
from j in Enumerable.Range(0, b.Count)
where a[i] == b[j]
select a[i];
var cList = c.ToList();
But it's much nicer to do:
var c = from aItem in a
join bItem in b on aItem equals bItem
select aItem;
var cList = c.ToList();
But this doesn't filter duplicates. To filter duplicates completely, you can do:
var cList = a.Intersect(b).ToList();
If you want duplicates to show up as many times as they do in b, for example:
var aSet = new HashSet<int>(a);
var cList = b.Where(aSet.Contains)
.ToList();
This is my version of intersection:
var a = new List<int>();
var b = new List<int>();
// intersection
var c = a.Where(x => b.Any(y => x == y)).ToList();
As Chris mentions in his comment on the original question, the sample code provided will return duplicates in list c (see his comment for details). Intersect will only return distinct values. To duplicate the behavior of the original sample code, try this:
var c = (from value in a
where b.Contains(a)
select a);
Related
in List<file> i have data :
id Initial B
1 G (2016-27-12)
2 H (2016-27-15)
3 G (2016-27-16)
//my code
List<file> i = new List<file>;
var r = i.Select(i=> i.Initial).GroupBy(x => new { r = x.to List() });
for( int i = 0; i < r.Count(); i++ )
{
comboBox1.Items.Add(r[i].ToString());
}
but my code still error.
how to GroupBy() with linq and each Initial result 2 count value G & H?
You can use
var r = i.Select(i=> i.Initial).GroupBy(x =>x).ToList();
Other way with Distinct()
var r = i.Select(i=> i.Initial).Distinct().ToList();
I dont know if thats what you are trying to do but to me it looks like you want to get every unique Initial from your list. To accomplish that, you can use "Distinct":
var r = i.Select(i=> i.Initial).Distinct();
If this is not what you are trying to do, please provide more info.
Trying to iterate through a session key I have, collect the values into a list, and then compare it to a database.
I have tried:
List<Model> listVar = new List<Model>();
for(int i = 0; i < ids.Count; i++)
{
int index = arrayValue[i]
listVar = databasemodel.table.Where(s => s.id == index).ToList()
}
It's only grabbing one of the values though when I do this, kinda new to Linq. Is there a method I can use instead of what I am doing now?
I had a simlar issue before, I used the .Contains() method.. as follows:
.Where(s => id.Contains(s.id));
That should work.
Supposing the s.id is an integer then you need to add the results of your WHERE expression to the final list
var selectedids = new List<int>();
for(int i = 0; i < ids.Count; i++)
{
int index = arrayValue[i];
selectedIds.AddRange(databasemodel.table.Where(s => s.id == index));
}
This is 2D array:
int[][] array2D = new int[7][];
for (int i = 0; i < 7; i++)
array2D[i] = new int[7];
How can I turn the following into a LINQ query, or use enumerable methods to achieve the same output?
var lst = new List<Point>();
for (int r = 0; r < array2D.Length; r++)
for (int c = 0; c < array2D[r].Length; c++)
if (array2D[r][c] == 0)
lst.Add(new Point(c, r));
EDIT - Solution based on #'King King's answer
var lst = m_boardArr.SelectMany((row, rowIndex) =>
row.Select((val, colIndex) =>
new { val, point = new Point(colIndex, rowIndex) })
.Where(col => col.val == 0)
.Select(col => col.point)).ToList();
Try this:
var lst = array2D.SelectMany((x,r) => x.Select((a,c)=> new {a,b=new Point(c,r)})
.Where(a=>a.a==0)
.Select(a=>a.b)).ToList();
The trick is to use the Select and SelectMany that capture the loop variables into an anonymous type, then get those properties back later after the Where clause, thus:
var list = array2D
.SelectMany((row, r) => row
.Select((el, c) =>
new {Element = el, ColIndex = c, RowIndex = r})
.Where(thing => thing.Element == 0)
.Select(thing => new Point(thing.RowIndex, thing.ColIndex)))
.ToList();
EDIT: Bartosz's comment applies to this solution as well. Unreadable!
var lst = array2D
.SelectMany((innerArray, r)
=> Enumerable
.Range(0, innerArray.Length)
.Where(c => innerArray[c] == 0)
.Select(c => new Point(c, r)))
.ToList();
However, your current solution is more readable.
int x = 9;
List<string> list = new List<string> {"a", "b"};
I want list to be: a b a b a ... until list.Count = x. How might I achieve this?
You could do it with LINQ easily:
List<string> result = (from ignored in Enumerable.Range(0, int.MaxValue)
from item in list
select item).Take(count).ToList();
Or without using a query expression:
List<string> result = Enumerable.Range(0, int.MaxValue)
.SelectMany(ignored => list)
.Take(count)
.ToList();
The use of Enumerable.Range here is just to force repetition - like Ani's approach of using Enumerable.Repeat, which will work too of course.
How about:
var result= Enumerable.Repeat(new[] { "a", "b" }, int.MaxValue)
.SelectMany(strArray => strArray)
.Take(x)
.ToList();
Something like this should work. I did not check it, let it be an exercise for you :)
int currentCount = list.Count;
for (int i=0; i<x; ++i)
{
list.Add(list[i%currentCount]);
}
int x = 9;
List<string> list = new List<string> {};
for (int i = 0; i < x; i++)
{
list.Add("a");
list.Add("b");
}
// verify
foreach (var item in list)
{
Console.WriteLine(item);
}
i have two list want to compare and then filter it.
so if list1 contains 1,2,3,4 and list2 2,4 i want to filter list and leave only 1 and 3
HProDataContext db = new HProDataContext();
List<int> _AllRoomsID = (from d in db.rooms select d.id).ToList();
List<int> _ClosedRoomsID = (from d in db.checkinouts select d.roomid).ToList();
for (int i = 0; i < _ClosedRoomsID.Count; i++)
{
if (_AllRoomsID.Contains(_ClosedRoomsID[i]) == true)
{
}
}
var OpenRooms = AllRoomsID.Except(ClosedRoomsID);
Or, if you don't want an IEnumearble:
List<int> OpenRooms = AllRoomsID.Except(ClosedRoomsID).ToList();