Reputation: 149
I have a problem with checking for list equality. I have the following list:
List<RequestDetailViewModel> requestDetail
that RequestDetailViewModel is :
public int PropertiesValueID { get; set; }
public int UnitID { get; set; }
public string Value { get; set; }
public int PropertyID { get; set; }
and i have another list "reqList":
var reqList = (from p in db.RequestDetail
group new
{ p.PropertyID, p.UnitID , p.Value , p.PropertiesValueID }
by p.RequestID into reqG
select reqG
);
i want to check list equality Like this:
foreach (var item in reqList)
{
if (requestDetail equals item)
{
return true;
}
}
How can I solve this?
Upvotes: 0
Views: 88
Reputation: 1267
I would update the RequestDetailViewModel
as follows:
public class RequestDetailViewModel : INotifyPropertyChanged
{
private ResultDetail resultDetail;
public RequestDetailViewModel(ResultDetail resultDetail)
{
this.resultDetail = resultDetail;
}
public ResultDetail
{
get
{
return this.resultDetail;
}
}
public int PropertiesValueID
{
get
{
return this.resultDetail.PropertiesValueID;
}
set
{
this.resultDetail.PropertiesValueID = value;
this.RaisePropertyChanged("PropertiesValueID");
}
}
public int UnitID
{
get
{
return this.resultDetail.UnitID ;
}
set
{
this.resultDetail.UnitID = value;
this.RaisePropertyChanged("UnitID");
}
}
public string Value
{
get
{
return this.resultDetail.Value;
}
set
{
this.resultDetail.Value= value;
this.RaisePropertyChanged("Value");
}
}
public int PropertyID
{
get
{
return this.resultDetail.PropertyID ;
}
set
{
this.resultDetail.PropertyID = value;
this.RaisePropertyChanged("PropertyID");
}
}
}
I'd also implement IEquatable in your DB model. and I'd do the check like this:
foreach (var item in reqList)
{
if (requestDetail.ResultDetail.Equals(item))
{
return true;
}
}
or better yet
return reqList.Any(item=> item.Equals(requestDetail.ResultDetail));
Upvotes: 0
Reputation: 2920
Why don't you loop your View Model list, linq-querying the domain list for each item. Something like:
foreach (var detail in requestDetail)
{
var reqListEqualItems = (from p in db.RequestDetail
where p.PropertyID == details.PropertyID &&
p.UnitID == details.UnitID &&
p.Value == details.Value &&
p.PropertyID == details.PropertyID);
}
Something like that should give you the Domain Object items for each ViewModel item.
Upvotes: 0
Reputation: 943
Modify your reqList select:
var reqList = (from p in db.RequestDetail
group new
{ p.PropertyID, p.UnitID , p.Value , p.PropertiesValueID }
by p.RequestID into reqG
select new RequestDetailViewModel{
PropertyID = reqG.PropertyID, UnitID = reqG.UnitID ,
Value = reqG.Value ,
PropertiesValueID = reqG.PropertiesValueID
});
Will return List<RequestDetailViewModel>
Implement IComparable
for your RequestDetailViewModel
class then use SequenceEqual
to compare two lists
Upvotes: 2