Scan Lists of Objects and compare an Object Value
up vote
-1
down vote
favorite
For example I have 3 lists (or more):
List1:
[store:"store1",item:"item1",price:10,store:"store1",item:"item2",price:5,store:"store1",item:"item4",price:100,store:"store1",item:"item10",price:10]
List2:
[store:"store2",item:"item1",price:15,store:"store2",item:"item2",price:10,store:"store2",item:"item10",price:110]
List3:
[store:"store3",item:"item1",price:5,store:"store3",item:"item2",price:10,store:"store3",item:"item10",price:100,store:"store3",item:"item100",price:1]
As you can see It's like 3 stores with different items and prices. Not all stores have all items so I would like to make a list by comparing the lists and finding the objects that contain "item1" for example and then choose the cheaper price. And also to compare the lists 1 by one (list 1 with list 2 , list 1 with list 3, list 2 with 1 and list 2 with 3). Do I make any sense?
Any answer is appreciated.
I've tried some things but I just cant understand it (and its for 2 stores):
var result = (from l1 in store1list join l2 in store2list on l1.Symbol equals l2.Symbol orderby l1.Symbol select new
store = l1.store,
price = l1.price,
item = l1.item
).ToList();
c# list object
add a comment |
up vote
-1
down vote
favorite
For example I have 3 lists (or more):
List1:
[store:"store1",item:"item1",price:10,store:"store1",item:"item2",price:5,store:"store1",item:"item4",price:100,store:"store1",item:"item10",price:10]
List2:
[store:"store2",item:"item1",price:15,store:"store2",item:"item2",price:10,store:"store2",item:"item10",price:110]
List3:
[store:"store3",item:"item1",price:5,store:"store3",item:"item2",price:10,store:"store3",item:"item10",price:100,store:"store3",item:"item100",price:1]
As you can see It's like 3 stores with different items and prices. Not all stores have all items so I would like to make a list by comparing the lists and finding the objects that contain "item1" for example and then choose the cheaper price. And also to compare the lists 1 by one (list 1 with list 2 , list 1 with list 3, list 2 with 1 and list 2 with 3). Do I make any sense?
Any answer is appreciated.
I've tried some things but I just cant understand it (and its for 2 stores):
var result = (from l1 in store1list join l2 in store2list on l1.Symbol equals l2.Symbol orderby l1.Symbol select new
store = l1.store,
price = l1.price,
item = l1.item
).ToList();
c# list object
2
What did you try already? Where did you get stuck?
– Klaus Gütter
Nov 10 at 20:35
Ill add it to the question
– Kjut Nikolas
Nov 10 at 20:35
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
For example I have 3 lists (or more):
List1:
[store:"store1",item:"item1",price:10,store:"store1",item:"item2",price:5,store:"store1",item:"item4",price:100,store:"store1",item:"item10",price:10]
List2:
[store:"store2",item:"item1",price:15,store:"store2",item:"item2",price:10,store:"store2",item:"item10",price:110]
List3:
[store:"store3",item:"item1",price:5,store:"store3",item:"item2",price:10,store:"store3",item:"item10",price:100,store:"store3",item:"item100",price:1]
As you can see It's like 3 stores with different items and prices. Not all stores have all items so I would like to make a list by comparing the lists and finding the objects that contain "item1" for example and then choose the cheaper price. And also to compare the lists 1 by one (list 1 with list 2 , list 1 with list 3, list 2 with 1 and list 2 with 3). Do I make any sense?
Any answer is appreciated.
I've tried some things but I just cant understand it (and its for 2 stores):
var result = (from l1 in store1list join l2 in store2list on l1.Symbol equals l2.Symbol orderby l1.Symbol select new
store = l1.store,
price = l1.price,
item = l1.item
).ToList();
c# list object
For example I have 3 lists (or more):
List1:
[store:"store1",item:"item1",price:10,store:"store1",item:"item2",price:5,store:"store1",item:"item4",price:100,store:"store1",item:"item10",price:10]
List2:
[store:"store2",item:"item1",price:15,store:"store2",item:"item2",price:10,store:"store2",item:"item10",price:110]
List3:
[store:"store3",item:"item1",price:5,store:"store3",item:"item2",price:10,store:"store3",item:"item10",price:100,store:"store3",item:"item100",price:1]
As you can see It's like 3 stores with different items and prices. Not all stores have all items so I would like to make a list by comparing the lists and finding the objects that contain "item1" for example and then choose the cheaper price. And also to compare the lists 1 by one (list 1 with list 2 , list 1 with list 3, list 2 with 1 and list 2 with 3). Do I make any sense?
Any answer is appreciated.
I've tried some things but I just cant understand it (and its for 2 stores):
var result = (from l1 in store1list join l2 in store2list on l1.Symbol equals l2.Symbol orderby l1.Symbol select new
store = l1.store,
price = l1.price,
item = l1.item
).ToList();
c# list object
c# list object
edited Nov 10 at 20:46
asked Nov 10 at 20:27
Kjut Nikolas
173
173
2
What did you try already? Where did you get stuck?
– Klaus Gütter
Nov 10 at 20:35
Ill add it to the question
– Kjut Nikolas
Nov 10 at 20:35
add a comment |
2
What did you try already? Where did you get stuck?
– Klaus Gütter
Nov 10 at 20:35
Ill add it to the question
– Kjut Nikolas
Nov 10 at 20:35
2
2
What did you try already? Where did you get stuck?
– Klaus Gütter
Nov 10 at 20:35
What did you try already? Where did you get stuck?
– Klaus Gütter
Nov 10 at 20:35
Ill add it to the question
– Kjut Nikolas
Nov 10 at 20:35
Ill add it to the question
– Kjut Nikolas
Nov 10 at 20:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
You may Union your lists and then GroupBy item, and select ordering each group with price and taking the first one (cheapest) from each group.
var result = List1.Concat(List2).Concat(List3).GroupBy(x => x.item)
.Select(g => g.OrderBy(x=> x.price).First()).ToList();
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You may Union your lists and then GroupBy item, and select ordering each group with price and taking the first one (cheapest) from each group.
var result = List1.Concat(List2).Concat(List3).GroupBy(x => x.item)
.Select(g => g.OrderBy(x=> x.price).First()).ToList();
add a comment |
up vote
2
down vote
You may Union your lists and then GroupBy item, and select ordering each group with price and taking the first one (cheapest) from each group.
var result = List1.Concat(List2).Concat(List3).GroupBy(x => x.item)
.Select(g => g.OrderBy(x=> x.price).First()).ToList();
add a comment |
up vote
2
down vote
up vote
2
down vote
You may Union your lists and then GroupBy item, and select ordering each group with price and taking the first one (cheapest) from each group.
var result = List1.Concat(List2).Concat(List3).GroupBy(x => x.item)
.Select(g => g.OrderBy(x=> x.price).First()).ToList();
You may Union your lists and then GroupBy item, and select ordering each group with price and taking the first one (cheapest) from each group.
var result = List1.Concat(List2).Concat(List3).GroupBy(x => x.item)
.Select(g => g.OrderBy(x=> x.price).First()).ToList();
edited Nov 10 at 20:51
answered Nov 10 at 20:36
Ashkan Mobayen Khiabani
19.2k1563114
19.2k1563114
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243110%2fscan-lists-of-objects-and-compare-an-object-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What did you try already? Where did you get stuck?
– Klaus Gütter
Nov 10 at 20:35
Ill add it to the question
– Kjut Nikolas
Nov 10 at 20:35