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

2

1 Answer 1

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())
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much @SalahAkbari

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.