1
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {

        string Card = (Request.Params["Card"]);
        DateTime Date = DateTime.Parse(Request.Params["Date"]);

        using ( AttendanceContext db = new AttendanceContext())
        {
            lblEmpName.Text = db.users.Where(t => t.Card == Card).SingleOrDefault().EmployeeName;
            lblDate.Text = Date.ToString("dd/MM/yyyy");
            if (lblEmpName.Text == null)
            {
                lblEmpName.Text = "No Data";
            }
            if (lblDate.Text == null)
            {
                lblDate.Text = "No Date";
            }

            var firstArray = db.TimeoutJustification.Where(x => x.Date == Date && x.Card == Card && x.GeneralJustification != null).ToList();

            var SecondArray = GetTimeOutData(Card, Date).OrderByDescending(t => t.Date).ToList();

            ///This is where i need to check both arrays
            var filtered = firstArray.Except(SecondArray);
            //var d = newTimeoutdata.Where(t => !newTimeoutdata.Contains(t.TimeOut.ToString())).ToList();
            if(filtered!=null){
                //This is where i will insert the unmatched array
                db.TimeoutJustification.AddRange(filtered);
                db.SaveChanges();
            }
        }

    }
}
}

My first array contains 3 items that are identical to 3 of the items in my second array.

I need to get the 4th unmatched item from my second array only. keep in mind that I need to compare both arrays a using datetime TimeOut since date and card will always be the same in all arrays

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Sam
  • 25
  • 8
  • 1
    Does this answer your question? [c# linq - get elements from array which do not exist in a different array](https://stackoverflow.com/questions/8975969/c-sharp-linq-get-elements-from-array-which-do-not-exist-in-a-different-array) – Heretic Monkey Dec 11 '19 at 14:05
  • Or [Getting the “diff” between two arrays in C#?](https://stackoverflow.com/q/683310/215552), or [Use LINQ to get items in one List, that are not in another List](https://stackoverflow.com/q/3944803/215552), or [Using LINQ to Objects to find items in one collection that do not match another](https://stackoverflow.com/q/1647698/215552), or [LINQ - Find all items in one list that aren't in another list](https://stackoverflow.com/q/11418942/215552) – Heretic Monkey Dec 11 '19 at 14:21

1 Answers1

1

You can use the Except method, something like this:

secondArray.Except(firstArray)

However you might also need to use a custom equality comparer for this purpose. I am not sure about your properties inside your class, you can do something like this for instance:

public class DistinctItemComparer : IEqualityComparer<yourClass>
{

    public bool Equals(yourClass x, yourClass y)
    {
        return x.Id == y.Id &&
            x.Date == y.Date &&
            x.Card == y.Card;
    }

    public int GetHashCode(yourClass obj)
    {
        return obj.Id.GetHashCode() ^
            obj.Date.GetHashCode() ^
            obj.Card.GetHashCode();
    }
}

And then using it like this:

secondArray.Except(firstArray, new DistinctItemComparer())
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109