I have two lists, one fake and one real, like:
BEFORE
// fake (list 1)
{ ID = 1, Year = 2011, X = "" }
, { ID = 2, Year = 2012, X = "" }
, { ID = 3, Year = 2013, X = "" }
// real (list 2)
{ ID = 35, Year = 2011, X = "Information" }
, { ID = 77, Year = 2013, X = "Important" }
I want to merge them looking for the Year, the result should be:
AFTER
{ ID = 35, Year = 2011, X = "Information" }
, { ID = 2, Year = 2012, X = "" }
, { ID = 77, Year = 2013, X = "Important" }
It must remove elements with the same year on the first list and add the element with the equivalent Year on the second list to the first list, keeping the order.
How can I do it using Linq?
You should be able to do that using a "left join":
from f in fake
join r in real
on f.Year equals r.Year
into joinResult
from r in joinResult.DefaultIfEmpty()
select new
{
ID = r == null ? f.ID : r.ID,
Year = f.Year,
X = r == null ? f.X : r.X
};
Justin's query is the most efficient way to do it, but if you're concerned with keeping identical objects (and not creating new records from the query) you could do it like this:
var combined = from f in fake
let r = (from r1 in real
where r1.Year == f.Year
select r1).SingleOrDefault()
select r ?? f;
Using IEnumerable.Union and IEqualityComparer.
P.S. This would result in a different result when compared to left join if the real list had more elements (years that are not present in fake list). The left join would not return those results which could be a desired result (not clear from OP).
public class MyClass
{
public int ID {get; set;}
public int Year {get; set;}
public string X {get; set;}
}
public class MyClassEqualityComparer : IEqualityComparer<MyClass>
{
public bool Equals(MyClass x, MyClass y)
{
return x.Year == y.Year;
}
public int GetHashCode(MyClass obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
void Main()
{
var fake = new List<MyClass> {
new MyClass { ID = 1, Year = 2011, X = "" }
, new MyClass { ID = 2, Year = 2012, X = "" }
, new MyClass { ID = 3, Year = 2013, X = "" }
};
var real = new List<MyClass> {
new MyClass { ID = 35, Year = 2011, X = "Information" }
, new MyClass { ID = 77, Year = 2013, X = "Important" }
};
var merged = real.Union(fake, new MyClassEqualityComparer());
}
Instead of defining the fake list yourself, try having Linq do it for you:
Enumerable.Range(2011,3) //2011, 2012, 2013
//use the overload that provides a 0-based ordinal position of each element
.Select(x,i=> new {ID = i+1, Year = x, X = String.Empty)
//now you have your fake list; join with the "real" list based on Year fields,
//taking the real element wherever it exists and the fake one otherwise
.Join(real, l=>l.Year, r=>r.Year, (l,r) => r == null ? l : r);
This will produce exactly the result set you want. You will likely need to define a named type for the list items, though, as two separately-defined anonymous types cannot be implicitly converted even if they have all the same member types/names.
Related
I would like to Lambda my code but am stuck.
Basically:
If the array object contains say 4 members with their own year specification and id's. The array can however contain many more members with the same and different Id's and year (never same Id and same year though).
Member array:
array[0]: Id 1 Year 2010
array[1]: Id 2 Year 2010
array[2]: Id 1 Year 2008
array[3]: Id 1 Year 2009
First -
I want to delete all array-members with a specific Id for the year 2010 if they also have another year in the array (same id, different year). So in this case I would like to delete the [0] but not the other members.
Secondly -
I want only to keep the next newest year after 2010 in this case for Id 1 the year 2009, meaning I want to delete [2] as well. (the years come as strings which is why I'm converting them into ints for the comparision in the code below)
Below is my code with for loops that work that I need expert Lambda help with to avoid the loops:
var x = Member.Length;
for (int i = 0; i < x; i++)
{
var y = Member[i].id;
for (int j = i; j < x; j++)
{
var z = Member[j].id;
if (i != j)
{
if (y == z)
{
if (Member[i].year == "2010")
{
Member = Member.Where(w => w != Member[i]).ToArray();
i--;
j--;
x--;
break;
}
var tempI = Convert.ToInt32(Member[i].year);
var tempJ = Convert.ToInt32(Member[j].year);
if (tempI > tempJ)
{
Member = Member.Where(w => w != Member[j]).ToArray();
i--;
j--;
x--;
break;
}
}
}
}
}
I agree that the requirement doesn't make a lot of sense but this is how I interpreted
var Member = new[]
{
new { id = 1, year = "2010" },
new { id = 2, year = "2010" } ,
new { id = 1, year = "2008" } ,
new { id = 1, year = "2009" }
};
var results = from item in Member.Select(x => new { x.id, Year = Convert.ToInt32(x.year), item = x })
group item by item.id into sameItems
let order = sameItems.OrderByDescending(x => x.Year)
let first = order.ElementAtOrDefault(0)
let second = order.ElementAtOrDefault(1)
select first.Year == 2010 && second != null ? second.item : first.item;
foreach (var item in results)
{
System.Console.WriteLine($"id:{item.id},year:{item.year}");
}
I tend to avoid using LINQ to change the underlying collection I'm querying.
The code below will select up to two of most recent entries for each member.
var result = new List<MemberClass>();
var groups = Member.OrderBy(m => m.Id).ThenByDescending(m => m.Year).GroupBy(m => m.Id).ToList();
groups.ForEach(c => result.AddRange(c.Take(2)));
Use result instead of the original array.
I don't know if performance is a consideration for you. The code above may become slow as your collection grows.
Your description and requirements are incompatible, but here's one interpretation:
public class Member
{
public int Id { get; set; }
public string Year { get; set; }
}
var items = (new List<Member>() {
new Member() { Id=1, Year="2010" },
new Member() { Id=2, Year="2010" },
new Member() { Id=1, Year="2008" },
new Member() { Id=1, Year="2009" }
}).ToArray();
// Group everythnig by year, then only keep the highest id
var firstFiltered = items
.GroupBy(
x => x.Year,
x => x.Id,
(year, ids) => new Member()
{
Id = ids.Last(),
Year = year
});
var secondFiltered = firstFiltered
// Only keep years before 2010
.Where(x => String.Compare(x.Year, "2010") == -1)
// Then order by Id then Year
.OrderBy(x => x.Id)
.ThenBy(x => x.Year)
// And only keep the last/most recent year
.GroupBy(
x => x.Id,
x => x.Year,
(id, years) => new Member()
{
Id = id,
Year = years.Last()
});
In C#,I have List of Employee object. Employee class is
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
In List objected are sorted based on Employee.ID. I have an array of int which is basically Employee.ID which I want on top of the list and in list,order must remain same as in array.
If I hava input like this
List:
[
{ID:1,Name:A},
{ID:2,Name:B},
{ID:3,Name:AA},
{ID:4,Name:C},
{ID:5,Name:CD},
.
.
{ID:100,Name:Z}
]
and Array: {2,3,1}
Then I want Output List:
[
{ID:2,Name:B},
{ID:3,Name:AA},
{ID:1,Name:A},
{ID:4,Name:C},
{ID:5,Name:CD},
.
.
{ID:100,Name:Z}
]
And I have done this
foreach (int i in a)
{
list = list.OrderBy(x => x.ID != i).ToList();
}
//a is array
//list is List
Any better Solution.Thanks in advance.
After you got your list sorted based on the ID just iterate the array and move the elements. In order to do this you need to first remove and then insert the item at the correct position.
for(int i = 0; i < myArray.Length; i++)
{
var e = myList.Single(x => x.Id == myArray[i]);
myList.Remove(e);
myList.Insert(i, e);
}
You may also want to use SingleOrDefault instead of Single to verify that myList even contains the element with the current id, e.g. when your array contains [2, 3, 101]
To add another version to the mix. The complete sorting can be done in one go:
list = list.OrderBy(e=> {int i =Array.IndexOf(a, e.ID); return i == -1 ? int.MaxValue : i; }).ToList();
where list is the EmployeeList and a the indices array. (NB, the for loop is not needed, the above should do both sortings).
Inside the OrderBy callback, if the id is not inside a, int.MaxValue is returned to place it after the ones inside the array (a.Length would work as well). OrderBy should maintain the original order of the enumeration (list) for those elements that return the same value.
PS, if you want to sort first by index inside a and the rest on the ids (not necessarily the original order), you can use the following (as long as a.Length + largest ID < int.MaxValue) : list = list.OrderBy(e=> {int i =Array.IndexOf(a, e.ID); return i == -1 ? a.Length + e.ID : i; }).ToList();
Here's a way to do it in pure LINQ, without changing the original sequence.
Broken into steps to see what's going on.
public static void Main()
{
var employeeList = new List<Employee>()
{
new Employee(){ ID= 1,Name= "A"},
new Employee() { ID= 2,Name= "B"},
new Employee() { ID= 3,Name= "AA"},
new Employee() { ID= 4,Name= "C"},
new Employee() { ID= 5,Name= "CD"},
new Employee() { ID= 100,Name= "Z"}
};
var orderByArray = new int[] { 2, 3, 1, 100, 5, 4 };
var sortPos = orderByArray.Select((i, index) => new { ID = i, SortPos = index });
var joinedList = employeeList.Join(sortPos, e => e.ID, sp => sp.ID, (e, sp) => new { ID = e.ID, Name = e.Name, SortPos = sp.SortPos });
var sortedEmployees = joinedList.OrderBy(e => e.SortPos).Select(e => new Employee { ID = e.ID, Name = e.Name });
}
Try this using LINQ:
List<Employee> employees = ...
int[] ids = ...
var orderEmployees = ids.Select(id => employees.Single(employee => employee.ID == id))
.Concat(employees.Where(employee => !ids.Contains(employee.ID)).ToList();
Foreach id in ids array we will grab the matching employee and we will concat to it all the employees that their id does not exist in ids array.
I like to use a special Comparer for that, it seems clearer to me, though a bit more code. It hides the complexity of the sort in the comparer class, and then you can just call it with :
theList.OrderBy(x => x.id, new ListOrderBasedComparer(sortList));
It will sort according to any list passed to the comparer when instantiating, and will put elements not in the "known sort list" at the end.
You can of course adapt it to your special needs.
public class ListOrderBasedComparer: Comparer<int>
{
private List<int> sortList;
public ListOrderBasedComparer(List<int> sortList)
{
// if you want you can make constructor accept arrays and convert it
// (if you find that more convenient)
this.sortList = sortList;
}
public override int Compare(int x, int y)
{
var indexOfX = sortList.FindIndex(a => a == x);
var indexOfY = sortList.FindIndex(a => a == y);
// handle elements not in sortArray : if not in sort array always assume they should be "less than the others" and "equal between them".
if (indexOfX == -1 && indexOfY == -1) return 0;
if (indexOfY == -1) return -1;
if (indexOfX == -1) return 1;
// if elements are in sortArray (FindIndex returned other than -1), use usual comparison of index values
return indexOfX.CompareTo(indexOfY);
}
}
Example on how to use it, with Linq :
public class TestCompare
{
public void test ()
{
var myArray = new MyClass[]
{
new MyClass { id = 1, name = "A" },
new MyClass { id = 2, name = "B" },
new MyClass { id = 3, name = "C" },
new MyClass { id = 4, name = "D" },
new MyClass { id = 5, name = "E" },
new MyClass { id = 6, name = "F" },
};
var myArray2 = new MyClass[]
{
new MyClass { id = 1, name = "A" },
new MyClass { id = 2, name = "B" },
new MyClass { id = 0, name = "X" },
new MyClass { id = 3, name = "C" },
new MyClass { id = 4, name = "D" },
new MyClass { id = 23, name = "Z"},
new MyClass { id = 5, name = "E" },
new MyClass { id = 6, name = "F" },
};
var sortList = new List<int> { 2, 3, 1, 4, 5, 6 };
// good order
var mySortedArray = myArray.OrderBy(x => x.id, new ListOrderBasedComparer(sortList)).ToList();
// good order with elem id 0 and 23 at the end
var mySortedArray2 = myArray2.OrderBy(x => x.id, new ListOrderBasedComparer(sortList)).ToList();
}
}
public class MyClass
{
public int id;
public string name;
}
Let's say I have following data:
Time Status
10:00 On
11:00 Off
12:00 Off
13:00 Off
14:00 Off
15:00 On
16:00 On
How could I group that using Linq into something like
[On, [10:00]], [Off, [11:00, 12:00, 13:00, 14:00]], [On, [15:00, 16:00]]
Create a GroupAdjacent extension, such as the one listed here.
And then it's as simple as:
var groups = myData.GroupAdjacent(data => data.OnOffStatus);
You could also do this with one Linq query using a variable to keep track of the changes, like this.
int key = 0;
var query = data.Select(
(n,i) => i == 0 ?
new { Value = n, Key = key } :
new
{
Value = n,
Key = n.OnOffFlag == data[i - 1].OnOffFlag ? key : ++key
})
.GroupBy(a => a.Key, a => a.Value);
Basically it assigns a key for each item that increments when the current item does not equal the previous item. Of course this assumes that your data is in a List or Array, otherwise you'd have to try a different method
Here is a hardcore LINQ solution by using Enumerable.Zip to compare contiguous elements and generate a contiguous key:
var adj = 0;
var t = data.Zip(data.Skip(1).Concat(new TimeStatus[] { null }),
(x, y) => new { x, key = (x == null || y == null || x.Status == y.Status) ? adj : adj++ }
).GroupBy(i => i.key, (k, g) => g.Select(e => e.x));
It can be done as.
Iterate over collection.
Use TakeWhile<Predicate> condition is text of first element of collection On or Off.
Iterate over the subset of from point one and repeat above step and concatenate string.
Hope it helps..
You could parse the list and assign a contiguous key e.g define a class:
public class TimeStatus
{
public int ContiguousKey { get; set; }
public string Time { get; set; }
public string Status { get; set; }
}
You would assign values to the contiguous key by looping through, maintaining a count and detecting when the status changes from On to Off and so forth which would give you a list like this:
List<TimeStatus> timeStatuses = new List<TimeStatus>
{
new TimeStatus { ContiguousKey = 1, Status = "On", Time = "10:00"},
new TimeStatus { ContiguousKey = 1, Status = "On", Time = "11:00"},
new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "12:00"},
new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "13:00"},
new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "14:00"},
new TimeStatus { ContiguousKey = 3, Status = "On", Time = "15:00"},
new TimeStatus { ContiguousKey = 3, Status = "On", Time = "16:00"}
};
Then using the following query you can extract the Status and grouped Times:
var query = timeStatuses.GroupBy(t => t.ContiguousKey)
.Select(g => new { Status = g.First().Status, Times = g });
I hope somebody will be able to guide me in right direction here...
public class SubmissionLog
{
public int PKId {get;set;}
public int SubmissionId {get;set;}
public DateTime Created {get;set;}
public int StatusId {get;set;}
}
And this is the data:
1, 123, '1/24/2013 01:00:00', 1
2, 456, '1/24/2013 01:30:00', 1
3, 123, '1/25/2013 21:00:00', 2
4, 456, '1/25/2013 21:30:00', 2
5, 123, '2/25/2013 22:00:00', 1
6, 123, '2/26/2013 21:00:00', 2
7, 123, '2/16/2013 21:30:00', 1
What I am trying to is following:
I'd like to know the the average time span from StatusId 1 to StatusId 2 on a given day.
So, let's say date is 2/26/2013, then what I thought would make sense if first get the list like this:
var endlingList = (from sl in db.SubmissionLogs
where (DateTime.Now.AddDays(days).Date == sl.Created.Date) // days = passed number of days to make it 2/26/2013
&& (sl.StatusId == 2)
select sl).ToList();
var endingLookup = endlingList.ToLookup(a => a.SubmissionId, a => a.Created); // thought of using lookup because Dictionary doesn't allow duplicates
After that I thought I'd figure out starting points
var startingList = (from sl in db.SubmissionLogs
where endingList.Select(a => a.SubmissionId).ToArray().Contains(sl.QuoteId)
&& sl.StatusId == 1
select sl).ToList();
And then what I did was following:
var revisedList = endingLookup.Select(a =>
new SubmissionInterval {
SubmissionId = a.Key,
EndDateTime = endingLookup[a.Key].FirstOrDefault(), //This is where the problem is. This will only grab the first occurance.
StartDateTime = startLookup[a.Key].FirstOrDefault() //This is where the problem is. This will only grab the first occurance.
});
And then what I do to get average is following (again, this will only include the initial or first ocurances of status 1 and status 2 of some submission id Submission Log):
return revisedList.Count() > 0 ? revisedList.Select(a=> a.EndDateTime.Subtract(a.StartDateTime).TotalHours).Average() : 0;
So, I hope somebody will understand what my problem here is first of all... To re-cap, I want to get timespan between each status 1 and 2. I pass the date in, and then I have to look up 2's as that ensures me that I will find 1's. If I went the other way around and looked for 1's, then 2's may not exist (don't want that anyway).
At the end I wanna be able to average stuff out...
So let's say if some submission first went from 1 to 2 in a time span of 5h (the code that I left, will get me up to this point), then let's say it got reassigned to 1 and then it went back to 2 in a new time span of 6h, I wanna be able to get both and do the average, so (5+6)/2.
Thanks
I think I understand what you're trying to do. Does thishelp
void Main()
{
var list = new List<SubmissionLog>
{
new SubmissionLog(1, 123, "1/24/2013 01:00:00", 1),
new SubmissionLog(2, 456, "1/24/2013 01:30:00", 1),
new SubmissionLog(3, 123, "1/25/2013 21:00:00", 2),
new SubmissionLog(4, 456, "1/25/2013 21:30:00", 2),
new SubmissionLog(5, 123, "2/25/2013 22:00:00", 1),
new SubmissionLog(6, 123, "2/26/2013 21:00:00", 2),
new SubmissionLog(7, 123, "2/16/2013 21:30:00", 1),
};
// split out status 1 and 2
var s1s = list.Where (l => l.StatusId == 1).OrderBy (l => l.Created);
var s2s = list.Where (l => l.StatusId == 2).OrderBy (l => l.Created);
// use a sub-query to get the first s2 after each s1
var q = s1s.Select (s1 => new
{
s1,
s2 = s2s.FirstOrDefault (s2 =>
s1.SubmissionId == s2.SubmissionId &&
s2.Created >= s1.Created
)
}
).Where (s => s.s1.PKId < s.s2.PKId && s.s2 != null);
// extract the info we need
// note that TotalSecond is ok in Linq to Object but you'll
// probably need to use SqlFunctions or equivalent if this is to
// run against a DB.
var q1 = q.Select (x => new
{
Start=x.s1.Created,
End=x.s2.Created,
SubmissionId=x.s1.SubmissionId,
Seconds=(x.s2.Created - x.s1.Created).TotalSeconds
}
);
// group by submissionId and average the time
var q2 = q1.GroupBy (x => x.SubmissionId).Select (x => new {
x.Key,
Count=x.Count (),
Start=x.Min (y => y.Start),
End=x.Max (y => y.End),
Average=x.Average (y => y.Seconds)});
}
public class SubmissionLog
{
public SubmissionLog(int id, int submissionId, string date, int statusId)
{
PKId = id;
SubmissionId = submissionId;
Created = DateTime.Parse(date, CultureInfo.CreateSpecificCulture("en-US"));
StatusId = statusId;
}
public int PKId {get;set;}
public int SubmissionId {get;set;}
public DateTime Created {get;set;}
public int StatusId {get;set;}
}
It is a little hard to explain it with my poor english but i will try.
In below list sequence, if a item first field has same value with another item first field value but not same second fields. As result i want to collect items which has same first field but not second fields.
It looks quite easy but i think it is not any.Consider that you will work on same sequence so it is important doing it effectively.
class MyClass
{
public int first;
public int second;
}
List<MyClass> sequence = new List<MyClass>();
Try this:
List<MyClass> sequence = new List<MyClass>()
{
new MyClass{ First = 1, Second = 10 },
new MyClass{ First = 1, Second = 10 },
new MyClass{ First = 2, Second = 11 },
new MyClass{ First = 2, Second = 12 }
};
var doesntMatch = sequence
.GroupBy(i => i.First)
.Select(g => new
{
Key = g.Key,
Values = g.Select(i => i.Second).Distinct()
})
.Where(i => i.Values.Count() > 1);
foreach (var i in doesntMatch)
{
Console.WriteLine(
"First = {0} contains {1} distinct values: {2}", i.Key, i.Values.Count(),
String.Join(", ", i.Values.Select(n => n.ToString()).ToArray()));
}
// output: "First = 2 contains 2 distinct values: 11, 12"
I'm thinking you might want to use GroupBy.
var sequence = new List<MyClass>()
{
new MyClass() { First = 1, Second = 2 },
new MyClass() { First = 1, Second = 3 },
new MyClass() { First = 1, Second = 4 },
new MyClass() { First = 3, Second = 2 },
new MyClass() { First = 5, Second = 4 },
};
var group1 = sequence.GroupBy(x => x.First);
you could do something like this with linq assuming you MyClass objects are in some kind of collection
Let's say a list<MyClass> myList for the example
(from o in myList where
(from o1 in myList where o1.first == o.first select o1).Count == 2
&& (from o2 in myList where o2.second == o.second select o2).count == 1
select o)
This says get all of the objects in my list where there are at least 2 objects that have the first parameter (o and some other object) and only one objects that have the second parameter.
I'm sure this could be improved upon.
I think that you could do this by joining the sequence to itself on the condition that the first field is equal. Below is some example code that does this. The output is also shown below. Note that this code results in duplicate matches found, so you may have to address that.
class Program
{
class MyClass
{
public int ID;
public int first;
public int second;
}
static void Main(string[] args)
{
// create a sequence containing example data
List<MyClass> sequence = new List<MyClass>();
sequence.AddRange(new MyClass[] {
new MyClass { ID = 1, first = 0, second = 10 },
new MyClass { ID = 2, first = 1, second = 11 },
new MyClass { ID = 3, first = 2, second = 12 },
new MyClass { ID = 4, first = 0, second = 10 },
new MyClass { ID = 5, first = 1, second = 20 },
new MyClass { ID = 6, first = 2, second = 30 },
new MyClass { ID = 7, first = 0, second = 0 },
new MyClass { ID = 8, first = 1, second = 11 },
new MyClass { ID = 9, first = 2, second = 12 },
});
var matches = from x in sequence
join y in sequence // join sequence to itself
on x.first equals y.first // based on the first field
where
!object.ReferenceEquals(x, y) // avoid matching an item to itself
&& x.second != y.second // find cases where the second field is not equal
select new { X = x, Y = y }; // return a "tuple" containing the identified items
foreach (var match in matches)
{
Console.WriteLine("Found first:{0}, x.second:{1}, y.second:{2}, x.ID:{3}, y.ID:{4}", match.X.first, match.X.second, match.Y.second, match.X.ID, match.Y.ID);
}
}
}
The output of this program is the following:
Found first:0, x.second:10, y.second:0, x.ID:1, y.ID:7
Found first:1, x.second:11, y.second:20, x.ID:2, y.ID:5
Found first:2, x.second:12, y.second:30, x.ID:3, y.ID:6
Found first:0, x.second:10, y.second:0, x.ID:4, y.ID:7
Found first:1, x.second:20, y.second:11, x.ID:5, y.ID:2
Found first:1, x.second:20, y.second:11, x.ID:5, y.ID:8
Found first:2, x.second:30, y.second:12, x.ID:6, y.ID:3
Found first:2, x.second:30, y.second:12, x.ID:6, y.ID:9
Found first:0, x.second:0, y.second:10, x.ID:7, y.ID:1
Found first:0, x.second:0, y.second:10, x.ID:7, y.ID:4
Found first:1, x.second:11, y.second:20, x.ID:8, y.ID:5
Found first:2, x.second:12, y.second:30, x.ID:9, y.ID:6
Here's what I came up with:
class MyClass
{
public int First;
public int Second;
}
void Main()
{
List<MyClass> sequence = new List<MyClass>()
{
new MyClass{ First = 1, Second = 10 },
new MyClass{ First = 1, Second = 10 },
new MyClass{ First = 1, Second = 11 },
new MyClass{ First = 2, Second = 11 },
new MyClass{ First = 2, Second = 12 },
new MyClass{ First = 3, Second = 10 }
};
var lonelyItems = sequence
// remove all those which don't match First
.GroupBy(x => x.First).Where(g => g.Count() > 1)
// keep only one for each Second
.SelectMany(g => g.GroupBy(x => x.Second)).Select(g => g.First());
foreach (var x in lonelyItems)
Console.WriteLine(x);
// output:
// 1,10
// 1,11
// 2,11
// 2,12
}