Scan Lists of Objects and compare an Object Value












-1














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();









share|improve this question




















  • 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
















-1














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();









share|improve this question




















  • 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














-1












-1








-1







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();









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















2














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();





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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();





    share|improve this answer




























      2














      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();





      share|improve this answer


























        2












        2








        2






        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();





        share|improve this answer














        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();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 20:51

























        answered Nov 10 at 20:36









        uɐʞɥsɐ

        20k1565114




        20k1565114






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini