In the past, i was reading data from one excel sheet named "On leave" and do operation on the result like this:
var resultNew1 = (from x in dataNew.Worksheet<Employee>("On Leave")
select x).ToList();
but now I have got another sheet which is "Working". so I did this:
var resultNew1 = (from x in dataNew.Worksheet<Employee>("On Leave")
select x);
var resultNew2 = (from x in dataNew.Worksheet<Employee>("Working")
select x);
please notice that the first one was ToList()
but now I didn't make ToLists() because I want to ask you if there is a way to store these two vars which are resultsNew1 and resultsNew2 in one var and then make that var ToList()
If you need more infomration, please tell me
You can use Concat
var employees = dataNew.Worksheet<Employee>("On Leave")
.Concat(dataNew.Worksheet<Employee>("Working"))
.ToList();
You can use List<T>.AddRange:
List<Employee> employees = new List<Employee>();
employees.AddRange(resultNew1);
employees.AddRange(resultNew2);
Related
I have struggled with it for a long time. I have two collections: MyRepository.All and MyCollection, both holds the collection of objects which has ID property. I need to get result of list of objects from MyRepository.All what contains only objects which id's are equal to MyCollection's objects'ids.
ICollection MyCollection // as parameter to method
var result = MyRepository.All.Where(r=>r.id==MyCollection.???.id).ToList();
i need to replace ??? with some linq to get this done.
ive tried different where and select caluses, excist and intersect and so on..
from a in MyRepository.All
join m in MyCollection on a.Id equals m.Id
select a
Cache the ids of MyCollection into a HashSet.
Than you can retrieve your result with a Where clause like this :
var myIdSets = new HashSet(MyCollection.Select(c => c.Id));
var result = MyRepository.All.Where(r=> myIdSets.Contains(r.id)).ToList();
var result = (from r in MyRepository.All
join r2 in MyCollection on r.id equals r2.id
select r).ToList();
MyRepository.All.Where(r=>MyCollection.Select(a=>a.id).Contains(r.id))
Linq has an .Intersect that should get you want you need.
Something like this:
var result = MyRepository.Intersect(MyCollection).ToList();
More info:
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect.aspx
Is there a way for me to filter Employees by group:
ex:
List<String> notInGroups = GetNotInGroups();
var list = from p in employees
where p.Group.Name notin(notInGroups)
select p;
Is there some way to do something like this?
Thanks
You can do !Contains, like:
var list = from p in employees
where !notInGroups.Contains(p.Group.Name)
select p;
Not able to test, but won't something like this work?
var notInGroups = GetNotInGroups();
var list = from p in employees
where notInGroups.Contains(p.Group.Name) == false
select p;
Try where !notInGroups.Contains(p.Group.Name); as your WHERE clause.
List is not particularly well suited for the task of searching through the collection to see if it contains a particular item, which is exactly what you want to do. While writing the code is easy enough (there are already a lot of answers showing how) you will benefit noticeably from using a more appropriate data structure that can be more efficiently searched, such as a HashSet:
var notInGroups = new HashSet<string>(GetNotInGroups());
var list = from p in employees
where !notInGroups.Contains(p.Group.Name)
select p;
You can do something like this..
List<String> notInGroups = GetNotInGroups();
var list = from p in employees
where !(notInGroups.Contains(p.Group.Name))
select p;
I asked this question earlier and that helped a lot. Then I realized I also need a list of IDs of the List that is a property in that object. Basically I want to end up with a list of integers generated from each list in those objects. Any ideas?
Thanks!
var ids = (from x in outerList
from y in x.List
select y).ToList();
Or to avoid dups:
var ids = (from x in outerList
from y in x.List
select y).Distinct().ToList();
For info, this could also be written:
var ids = outerList.SelectMany(x => x.List).ToList();
or:
var ids = outerList.SelectMany(x => x.List).Distinct().ToList();
I have linq query as follows:
var result = (from Customer cust in db select new { userNameList = cust.UserName }).ToList();
i want to loop through each value in the list<>
I tried to use the foreach to accomplish this. It is stupid i could not figure it out
I'm using something like this
foreach (List<string> item in result)
{
if (item.ToString() == userName)
{
userExistsFlag = 1;
}
}
But the .net compiler is just freaking out:
and giving me these errors
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'
Cannot convert type 'AnonymousType#1' to 'System.Collections.Generic.List'
Thanks in anticipation
OF ALL THESE IMPLEMENTATIONS WHICH ONE IS MOST EFFICIENT AND CONSUMES LESS RESOURCES.
IT WOULD BE KIND ENOUGH IF SOME ONE CAN CLARIFY THIS FOR ME.
Shorter using Linq:
bool userExistsFlag = result.Any( x=> x.userNameList == userName);
As suggested in the other answers you do not need to project to an anonymous type:
var userNames = (from Customer cust in db select cust.UserName).ToList();
bool userExists = userNames.Contains(userName);
Edit:
The most efficient - if you do not need the set of user names otherwise - is to query the DB directly to check whether the user name exists, so
bool userExists = db.Any( x => x.UserName == userName);
Credit goes to #Chris Shaffer in the comments and #Cybernatet's answer - he was almost there. I would suggest you accept his answer but use Any() ;-)
Try:
var result = (from Customer cust in db select new { userNameList = cust.UserName }).ToList();
userExistsFlag = result.Where(a=> a.userNameList == userName).Count() > 0;
or
userExistsFlag = (
from Customer cust in db
where cust.UserName = userName
select cust
).Count() > 0;
If your query returns a list of names, your FOREACH loop should look like this
foreach( String name in results ){
...
}
Skip using new { userNameList = cust.UserName } which is making it an anonymous instance. You can try
var result = (from Customer cust in db select cust.UserName ).ToList();
if you're just getting the one property and want a list of strings there is no reason to use an anonymous type. code should work like this:
var result = (from Customer cust in db select cust.UserName).ToList();
DataTable table = DataProvider.GetTable()
var clientIds = from r in table.AsEnumerable()
select r.Field<string>("CLIENT_ID");
I want clientIds to be a List<string>. Currently it's an EnumerableRowCollection<>
What am I missing?
this may work
DataTable table = DataProvider.GetTable()
var clientIds = (from r in table.AsEnumerable()
select r.Field<string>("CLIENT_ID")).ToList();
Here is one way to do it:
var clientIds = table.Rows.Cast<DataRow>().Select(r => r.Field<string>("CLIENT_ID").ToList();
Or, if this syntax is working but not bringing back the results as a list, you can do something like:
var clientIds = (from r in table.AsEnumerable()
select r.Field<string>("CLIENT_ID")).ToList();