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.
Related
I have a ID list as "100,1025,5341" and a objects list. I want to get the IDs which are not include in my object list.
as an example, lets assume that objects list contains both 100 and 5341 as a property (eg: ID). I want to get 1025 as my result. I know that this request make no sense but i want to get that somehow. I can easily use,
string idList = "100,1025,5341";
var objectList = _dataService.GetData();
var result = objectList.Any(item=> idList.Contains(item.ID));
to get the list items which are having given IDs. But i want get other way. I want get the IDs which are not exists in the list.
Use Except:
var ids= idList.Split(',').Select(int.Parse);
var result = objectList.Where(item=> ids.Contains(item.ID));
var r= objectList.Except(result);
If you want only the ids that are not include in your object list, then you can do the following
var ids= idList.Split(',').Select(int.Parse);
var r= ids.Except(ids.Where(i=> objectList.Any(item=> item.ID==i)));
It will get you Ids from idList which are not Ids of objectList. If I undestood correctly what you want...
var ids = objectList.Select(x=>x.ID).ToList();
var otherIdsList = idList.Split(',').Select(x=>int.Parse(x)).ToList().Where(x=> !ids.Contains(x)).ToList();
I am trying to define a LINQ response to this issue, I have a List<List<string>> and I need to compare the first line of the internal list and return that list based on the match:
List<List<string>> mainList = new List<List<string>>();
List<string>lines = new List<string>();
lines.Add("one");
lines.Add("two");
lines.Add("three");
mainList.Add(lines);
lines=new List<string>();
lines.Add("bus");
lines.Add("clock");
lines.Add("chicken");
mainList.Add(lines);
How do I use LINQ to return the entire list which contains "bus"?
If you want to compare any element in the Sub List then do:
List<string> subList = mainList.FirstOrDefault(r => r.Contains("bus"));
If you only want to compare first element in the Sub List then do:
List<string> subList = mainList.FirstOrDefault(r => r.FirstOrDefault() == "bus");
You could try this one:
var result = mainList.FirstOrDefault(x=>x.Contains("bus"));
The above query will search all the list contained in the mainList and it will return you the first List that contains the string "bus". If there wasn't such a list then you would have gotten a null.
I'm very beginner in C#.
I have a List of
private List<String> mXMLProdcutsIDs = new List<String>();
mXMLProductsIDs is like
{"megapack", "levelpack","bikepack"}.
Sometimes I get another list which is in random order, {"megapack", "levelpack","bikepack"}
I would like to resort that list according to the mXMLProductIDs list order
You can use index of string in original list to define items order:
var result = anotherList.OrderBy(s => mXMLProdcutsIDs.IndexOf(s));
Shorten version:
var result = anotherList.OrderBy(mXMLProdcutsIDs.IndexOf);
Keep in mind, that result will be IEnumerable<string>. You can use ToList() if you need to save results in list.
Another option (if you don't have duplicated items in another list):
var result = mXMLProdcutsIDs.Intersect(anotherList);
I have a list of result List where it contains List inside of it.I have another list where it contains List alone.I want to filter using a linq query from the data where it should return all the data which contains skill id from the second list.
var list = this._viewModel.Data.Select(T => T.SkillsList);
var filtered = item.Skills.Contains(list.Where(t=>t.ToString()).ToList();
from the first list it contains List of decimals inside the skill list;
item.Skills contains list where the fields are skillid and code.
item is another object which contains the skillslist.
if skillId is a variable and assuming that SkillsList contains a property called Id. Then the following would work in getting you any data that has the specified skillId.
var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=>s.Id == skillId));
If Skillslist is just an array of integers then the following would work.
var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=> s == skillId));
Now if you are checking against a list the following would work.
var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=> skillsList.contains(s));
I want to be able to return a list of all "list values" coming from the query.. 'query' below returns multiple rows of results back from db, each as an item in a list. A sample result back from db would look like...
sample query results when I put break point: (this is what first line of code below 'query' returns from db)
Name = John ; Address = 1230, Ewded ; listOfCities = "NY, CH, LA"
Name = Eric; Address = 12 , Ewded ; listOfCities = "BO, SE, OR"
Code:
List<Index.Result> query = getresultsbackfromdb();
// query content at this point looks like above 1,2
List<string> result = new List<string>();
foreach (var item in query)
{
results.Add(item.listCities);
//'results' list takes in string and not a list
//How do I return a consolidated list of items
}
return result; // this should have ""NY, CH, LA, BO, SE, OR"
//I am trying to get a list of all cities from 1,2 included in
//one single list.
There is method in a List that allows you to add multiple items
foreach (var item in query)
{
results.AddRange(item.listCities);
}
Docs for List.AddRange Method.
Also, just in case if you need to filter out some repeated items, you can use a Distinct LINQ method.
You can try this code based on Split Method
var result = yourString.Split(',');
var input = "NY, CH, LA";
var result = input.Split(',');
And you can save this value in List<object>
var list = new List<object>();
list.Add(result );
You want the AddRange and string.Split methods
results.AddRange(string.Split(',', item.ListCities));
string.Split will split the string into an array wherever it finds the given character, and add range will add all items in an array to the list.
Try this:
var result = query.SelectMany(x => x.listOfCities.Split(','));
Or use
var result = query.SelectMany(x => x.listOfCities.Split(',')).Distinct();
to get the list without duplicates.
If you like Linq then you could do this one line:
using System.Linq;
List<string> result = query.SelectMany(s => s.listCities).ToList();
(This does essentially the same thing as oleksii's AddRange.)