I have two string arrays for example selPhoto["419","418"]
and preview_photo["418"]
.I need to check whether each element in selPhoto
present in
preview_photo
or not in mvc4.
Asked
Active
Viewed 339 times
1
-
_"Insufficient data for meaningful answer"_ - Isaac Asimov. Any code to show? – Oct 09 '15 at 05:57
-
what is the code you have written for this so far? – Shetty Oct 09 '15 at 05:57
-
Hmmm...the person who upvoted this should perhaps read _[How do I ask a good question?](http://stackoverflow.com/help/how-to-ask)_ along with the OP – Oct 09 '15 at 06:18
-
Possible duplicate of [Check whether an array is a subset of another](http://stackoverflow.com/questions/332973/check-whether-an-array-is-a-subset-of-another) – 31eee384 Oct 11 '15 at 07:05
5 Answers
0
How about this:
var y = new[] { "419", "418" };
var x = new[] { "418" };
Check for intersection
x.Intersect(y).Contains("418");

Jim
- 14,952
- 15
- 80
- 167
0
You can try to check if one array is a subset of another:
bool isSubset = !array2.Except(array1).Any();
So it would be like
bool isSubset = !preview_photo.Except(selPhoto).Any();
You can try to create an extension method as well for this
public static bool isSubset<T>(this IEnumerable<T> arr1, IEnumerable<T> arr2)
{
return !arr1.Except(arr2).Any();
}

Rahul Tripathi
- 168,305
- 31
- 280
- 331
0
You can use Except
and check to see if there are any items in the resulting set
var containsAllElements = !preview_photo.Except(selPhoto).Any();

David Pilkington
- 13,528
- 3
- 41
- 73
0
public ActionResult AddtoCart(string selPhoto, string preview_photo)
{
string[] values = selPhoto.Split(',');
string[] photo = preview_photo.Split(',');
foreach (var item in values)
{
if (photo.Contains(item))
{
// do action item in second array
}
else
{
//do action item not in second array
}
}
}

neethu
- 65
- 7
0
var y = new[] { "419", "418" };
var x = new[] { "418" };
bool present=y.ToList().TrueForAll(a=>x.Contains(a));
however its always good to show what you have tried

Akshita
- 849
- 8
- 15