Take if from the top. have a list that I return MyTableList.
var MyTableList = List<ExportDto>(List);
this tableList returns ID's that is needed to select another level from the same table.
IEnumerable<int?> rowselect = MyTableList.Where(n => n.ID != null)
.Select(n => n.ID);
this provides me with a list of ID's are then returned;
I use a
foreach (var item in rowselect)
getnewlist = getnewinfo.GetSinglerows(item.Value);
The main problem is for me Adding this new list and merging it with MyTableList.
Use List<T>.AddRange Method as following:
MyTableList.AddRange(getnewlist );
Related
I have a list of users for a timespan, for arguments sake let's say a month. So in that list certain users do not meet the criteria I want for a certain objective so I want to filter out the list and display the filtered out data into another list. So how I replicated the list was as follows:
List<tblList1> List1 = new XPQuery<tblList1>(session)
.Where(w => w.UserCode != null).ToList();
I then use a foreach loop to go through this list to compare the data to my criteria and then add them to the new list which is working perfectly. The problem I now have is to delete the data I took from the first list. I tried the following in a new method which I created:
public void DeleteData(Session session)
{
List<tblList1> List1= new XPQuery<tblList1>(session)
.Where(w => w.UserID != null).ToList();
List<tblList2> List2= new XPQuery<tblList2>(session)
.Where(w => w.UserID!= null).ToList();
List1.RemoveAll(w => w.UserID == List2.Any(e => e.UserID== w.UserID));
}
So in the end I want to remove all the data in list1 so that we can view the deleted data in list2. Any help would be appreciated if I can just get the RemoveAll LINQ statement correct as the current line does not work and I am unsure of how to handle this in LINQ.
As far as I can see from comments:
I want to take List1 {1, 2, 3} compare them to criteria and see that {
2 } does not meet that criteria and add that in List2. Then delete { 2
} from List1
I can't see any need in Linq at all. Let's fill both Lists in parallel:
List<tblList1> List1 = new List<tblList1>();
//TODO: please, check types; it seems that it should be List<tblList1> List2
List<tblList2> List2 = new List<tblList2>();
foreach (var item in new XPQuery<tblList1>(session).Where(w => w.UserID != null)) {
if (YourCriteriaHere)
List1.Add(item); // <- Criteria met: add to List1
else
List2.Add(item); // <- Doesn't meet: "delete" from (just not add to) List1 into List2
}
You can just use LINQ Where to filter your list, and then Except to get unfiltered values:
List<Item> all = ...; // your original list
List<Item> matching = all.Where(x => IsMatching(x)).ToList(); // IsMatching is any filtering logic
List<Item> notMatching = all.Except(matching).ToList();
I have used two IList object. In first list, it contains 10 records and the second contains 5 records. Now I want to update the first IList with second IList data.
foreach (ObjList newlist in New)
{
ObjList list = ExtList.FirstOrDefault(x => x.Id == newlist.Id);
if (list!= null)
{
ExtList.Remove(list);
ExtList.Add(newlist);
}
}
I tried the above. But the added object appended at the end of the list. So sort order changed. I need it in the same existing order.
Updated
I tried the sorting, but it is not sorted.
ExtList.OrderBy(x => x.Id);
You can try using IList.Insert() to add new item at the index of to-be-replaced item, so that you can keep the list in it's original order :
ObjList list = ExtList.FirstOrDefault(x => x.Id == newlist.Id);
if (list!= null)
{
var position = ExtList.IndexOf(list);
ExtList.Insert(position, newlist);
ExtList.Remove(list);
}
If you want to keep the order after update, you can :
ExtList=(from e in ExtList
join n in New
on e.Id equals n.Id into left
from n in left.DefaultIfEmpty()
select new /*Class Name */
{
Id=e.Id,
Name=n==null?e.Name:n.Name
}).ToList();
You should use ExtList.IndexOf instead ExtList.FirstOrDefault.
And then use ExtList.Insert to given index instead ExtList.Add
I have a List defined as below
var MList = new List<KeyValuePair<String, Object>>();
(Example list item { "Prj1" , {e1,e2,e3} }
Ideally, a List item would contain a Project Name (string) and an array of admin ids (Object). I need to create and populate a new List with items which are expanded from the original list.
So the resulting list would look like below, with each item being a string.
{"Prj1","e1"}
{"Prj1","e2"}
{"Prj1","e3"}
How can i use LINQ to fetch items into the new list in the above format
This can be done using SelectMany, like this:
var expanded = MList.SelectMany(
item => ((IEnumerable<string>)item.Value).Select( str =>
new KeyValuePair<string,object>(item.Key, str)
)
);
The above assumes that Object contains an IEnumerable<string>.
Is your Object an IEnumerable?
var newlist = from x in mList
from v in (IEnumerable<object>)x.Value
select new
{
x.Key,
v,
};
I have a list of details about a large number of files. This list contains the file ID, last modified date and the file path. The problem is there are duplicates of the files which are older versions and sometimes have different file paths. I want to only store the newest version of a file regardless of file path. So I created a loop that iterates through the ordered list, checks to see if the ID is unique and if it is, it gets stored in a new unique list.
var ordered = list.OrderBy(x => x.ID).ThenByDescending(x => x.LastModifiedDate);
List<Item> unique = new List<Item>();
string curAssetId = null;
foreach (Item result in ordered)
{
if (!result.ID.Equals(curAssetId))
{
unique.Add(result);
curAssetId = result.ID;
}
}
However this is still allowing duplicates into the DB and I can't figure out why this code isn't working as expected. By duplicates I mean, the files have the same ID but different file paths, which like I said before shouldn't be an issue. I just want the latest version regardless of pathway. Can anyone else see what the issue is? Thanks
var ordered = listOfItems.OrderBy(x => x.AssetID).ThenByDescending(x => x.LastModifiedDate);
List<Item> uniqueItems = new List<Item>();
foreach (Item result in ordered)
{
if (!uniqueItems.Any(x => x.AssetID.Equals(result.AssetID)))
{
uniqueItems.Add(result);
}
}
this is what I have now and it is still allowing duplicates
This is because , you are not searching entire list to check whether the id is unique or not
List<Item> unique = new List<Item>();
string curAssetId = null; // here is the problem
foreach (Item result in ordered)
{
if (!result.ID.Equals(curAssetId)) // here you only compare the last value.
{
unique.Add(result);
curAssetId = result.ID; // You are only assign the current ID value and
}
}
to solve this , change the following
if (!result.ID.Equals(curAssetId)) // here you only compare the last value.
{
unique.Add(result);
curAssetId = result.ID; // You are only assign the current ID value and
}
to
if (!unique.Any(x=>x.ID.Equals(result.ID)))
{
unique.Add(result);
}
I don't know if this code is just simplified, but have you considered grouping on ID, sorting on LastModifiedDate, then just taking the first from each group?
Something like:
var unique = list.GroupBy(i => i.ID).Select(x => x.OrderByDescending(y => y.LastModifiedDate).First());
var ordered = list.OrderBy(x => x.ID).ThenByDescending(x => x.LastModifiedDate).Distinct() ??
For this purpose you have to create your own EquityComparer and after that you could use linq's Distinct method. Enumerable.Distinct at msdn
Also I think you could stay with your current code but you have to modify it in such a way (as a sample):
var ordered = list.OrderByDescending(x => x.LastModifiedDate);
var unique = new List<Item>();
foreach (Item result in ordered)
{
if (unique.Any(x => x.ID == result.ID))
continue;
unique.Add(result);
}
List<Item> p = new List<Item>();
var x = p.Select(c => new Item
{
AssetID = c.AssetID,
LastModifiedDate = c.LastModifiedDate.Date
}).OrderBy(y => y.id).ThenByDescending(c => c.LastModifiedDate).Distinct();
I have an
IEnumerable<typeA> result;
from this result I need to get sum group by some id.
So I have the query
var groupeddata = from data in result
group data by data.Title
into grouped
select new { intid= grouped.Key,
expsum= grouped.Sum(x=>x.expnum)};
now this expsum I need to assign to the items of result where typeA.id is same as intid. Now how to do this assignment?
The simplest approach would probably be to use a dictionary:
var sumDictionary = query.ToDictionary(pair => pair.intid, pair => pair.expsum);
foreach (var item in result)
{
// We don't know which property you actually want to assign to
item.Sum = sumDictionary[item.id];
}