Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Next Linq Question

Next Linq Question

Scheduled Pinned Locked Moved C#
questioncsharplinq
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have this method:

    public static List QueryInventoryParts(List<> SearchTerms, int ProducId = 0)
    {
    using (SparesDataContext context = getDataContext())
    {
    List<> retVal = new List<>();

        foreach (SearchItemModel searchModel in SearchTerms)
        { 
            List<\> results = (from pp in context.mmlProductsParts
                                        join pa in context.tblParts on pp.PartId equals pa.PartId
                                        where pa.PartNumber.Contains(searchModel.SearchTerm) ||
                                                pa.Keyword.Contains(searchModel.SearchTerm)
                                        select new PartModel
                                        {
                                            PartId = pa.PartId,
                                            Category = GetCategory(pa.CategoryId),
                                            PartNumber = pa.PartNumber,
                                            Keywords = pa.Keyword,
                                            PartDescription = pa.Description,
                                            Price = pa.Price.Value,
                                            Ratio = pa.Ratio.Value,
                                            Tribal = pa.Tribal
                                        }).ToList();
    
            foreach (var result in results)
            {
                retVal.Add(result);
            }
        }
        return retVal;
    }
    

    }

    If the ProductId is passed in, then I was to add "&& pp.ProductId = ProductId" to the WHERE clause, however, if Zero is passed in, I want to return all products. I'm not sure of the syntax on this. Can someone show me? Thanks

    Everything makes sense in someone's mind

    B N 2 Replies Last reply
    0
    • K Kevin Marois

      I have this method:

      public static List QueryInventoryParts(List<> SearchTerms, int ProducId = 0)
      {
      using (SparesDataContext context = getDataContext())
      {
      List<> retVal = new List<>();

          foreach (SearchItemModel searchModel in SearchTerms)
          { 
              List<\> results = (from pp in context.mmlProductsParts
                                          join pa in context.tblParts on pp.PartId equals pa.PartId
                                          where pa.PartNumber.Contains(searchModel.SearchTerm) ||
                                                  pa.Keyword.Contains(searchModel.SearchTerm)
                                          select new PartModel
                                          {
                                              PartId = pa.PartId,
                                              Category = GetCategory(pa.CategoryId),
                                              PartNumber = pa.PartNumber,
                                              Keywords = pa.Keyword,
                                              PartDescription = pa.Description,
                                              Price = pa.Price.Value,
                                              Ratio = pa.Ratio.Value,
                                              Tribal = pa.Tribal
                                          }).ToList();
      
              foreach (var result in results)
              {
                  retVal.Add(result);
              }
          }
          return retVal;
      }
      

      }

      If the ProductId is passed in, then I was to add "&& pp.ProductId = ProductId" to the WHERE clause, however, if Zero is passed in, I want to return all products. I'm not sure of the syntax on this. Can someone show me? Thanks

      Everything makes sense in someone's mind

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      where (...) && ((pp.ProductId == ProductId) || (ProductId == 0)) Or am I missing something?

      1 Reply Last reply
      0
      • K Kevin Marois

        I have this method:

        public static List QueryInventoryParts(List<> SearchTerms, int ProducId = 0)
        {
        using (SparesDataContext context = getDataContext())
        {
        List<> retVal = new List<>();

            foreach (SearchItemModel searchModel in SearchTerms)
            { 
                List<\> results = (from pp in context.mmlProductsParts
                                            join pa in context.tblParts on pp.PartId equals pa.PartId
                                            where pa.PartNumber.Contains(searchModel.SearchTerm) ||
                                                    pa.Keyword.Contains(searchModel.SearchTerm)
                                            select new PartModel
                                            {
                                                PartId = pa.PartId,
                                                Category = GetCategory(pa.CategoryId),
                                                PartNumber = pa.PartNumber,
                                                Keywords = pa.Keyword,
                                                PartDescription = pa.Description,
                                                Price = pa.Price.Value,
                                                Ratio = pa.Ratio.Value,
                                                Tribal = pa.Tribal
                                            }).ToList();
        
                foreach (var result in results)
                {
                    retVal.Add(result);
                }
            }
            return retVal;
        }
        

        }

        If the ProductId is passed in, then I was to add "&& pp.ProductId = ProductId" to the WHERE clause, however, if Zero is passed in, I want to return all products. I'm not sure of the syntax on this. Can someone show me? Thanks

        Everything makes sense in someone's mind

        N Offline
        N Offline
        NeillJam
        wrote on last edited by
        #3

        Hi, probably not the best solution but off the top of my head i can think of 2 methods. 1. Use a switch on ProductID - case 0: --> do query to return all default: --> do other query to return specifics matching ProductId 2.

        List<< PartModel>> results = **((ProductId != 0) ? ** (from pp in context.mmlProductsParts
        join pa in context.tblParts on pp.PartId equals pa.PartId
        where pa.PartNumber.Contains(searchModel.SearchTerm) ||
        pa.Keyword.Contains(searchModel.SearchTerm)
        && pp.ProductId == ProductId
        select new PartModel
        {
        PartId = pa.PartId,
        Category = GetCategory(pa.CategoryId),
        PartNumber = pa.PartNumber,
        Keywords = pa.Keyword,
        PartDescription = pa.Description,
        Price = pa.Price.Value,
        Ratio = pa.Ratio.Value,
        Tribal = pa.Tribal
        }).ToList()
        : (from pp in context.mmlProductsParts
        join pa in context.tblParts on pp.PartId equals pa.PartId
        where pa.PartNumber.Contains(searchModel.SearchTerm) ||
        pa.Keyword.Contains(searchModel.SearchTerm)
        select new PartModel
        {
        PartId = pa.PartId,
        Category = GetCategory(pa.CategoryId),
        PartNumber = pa.PartNumber,
        Keywords = pa.Keyword,
        PartDescription = pa.Description,
        Price = pa.Price.Value,
        Ratio = pa.Ratio.Value,
        Tribal = pa.Tribal
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups