Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • SinkyPars 131 posts 164 karma points
    Sep 21, 2011 @ 17:59
    SinkyPars
    0

    Linq 2 Umbraco Dynamic

    Hi,

    I have a little problem getting below to work.

    Basically I am trying to retrieve data dynamically but below give me a Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Steak.Article>' to 'System.Linq.IOrderedEnumerable<Steak.Article>'. An explicit conversion exists (are you missing a cast?) Error

    using (OADataContext con = new OADataContext())
            {
    
                var e = (from a in con.Articles where a.ArticleDate <= DateTime.Now orderby a.ArticleDate descending select a);
    
                int[] selections = ddJobSector.GetSelectedIndices();
    
                foreach (int id in selections)
                {
                    if (id != 0)
                    {
    
                        e = e.Where(x => x.ArticleSector == ddJobSector.Items[id].Text));
    
    
    
                    }
    
    
    
    
                }
    
            }

    Any help is much appreciated

    Thanks

    Scott

  • Owen 123 posts 246 karma points
    Sep 21, 2011 @ 18:19
    Owen
    0

    try

    dynamic e =(from a in con.Articleswhere a.ArticleDate<=DateTime.Noworderby a.ArticleDatedescendingselect a);
  • SinkyPars 131 posts 164 karma points
    Sep 21, 2011 @ 18:29
    SinkyPars
    0

    This does not seem to help. The problem is in the e = e.Where(x => x.ArticleSector== ddJobSector.Items[id].Text));

    Thanks

     

    Scott

  • Owen 123 posts 246 karma points
    Sep 21, 2011 @ 18:35
    Owen
    0

    have you have a try?

    according the code, you declare e in "var e =(from a in con.Articleswhere a.Artic....", you use keyword "var", so the type of e will be determied by his value,

    in this case it's System.Linq.IOrderedEnumerable.

    But "e.Where(x => x.ArticleSector== ddJobSector.Items[id].Text))" will return a "System.Collections.Generic.IEnumerable",  you can't assign a "System.Collections.Generic.IEnumerable" value to "System.Linq.IOrderedEnumerable" variable.

  • SinkyPars 131 posts 164 karma points
    Sep 21, 2011 @ 18:41
    SinkyPars
    0

    Hi,

    I know get the error - Error23 Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

     dynamic e = (from a in con.Articles where a.ArticleDate <= DateTime.Now orderby a.ArticleDate descending select a);

     

                int[] selections = ddJobSector.GetSelectedIndices();

     

                foreach (int id in selections)

                {

                    if (id != 0)

                    {

     

                        e = e.Where(x => x.ArticleSector == ddJobSector.Items[id].Text);

                        

     

                       

     

                    }

     

                    

     

     

                }

    Thanks

     

    Scott

  • SinkyPars 131 posts 164 karma points
    Sep 21, 2011 @ 18:46
    SinkyPars
    0

    Actually, I think what I really want to ask is how can I, using linq to umbraco create a dynamic WhereOr scenario?

     

    S

  • Owen 123 posts 246 karma points
    Sep 22, 2011 @ 03:01
    Owen
    0

    try this one:

    using (OADataContext con = new OADataContext())

            {

    int[] selections = ddJobSector.GetSelectedIndices();

            var e = (from a in con.Articles where a.ArticleDate <= DateTime.Now orderby a.ArticleDate descending select a)

    .Where(i=>{

    var result = true;

    foreach(int id in selections){

    if(id != 0){

    result = (x.ArticleSector == ddJobSector.Items[id].Text);

    }

    if(!result){

    break;

    }

    }

    return result;

    });

            }

Please Sign in or register to post replies

Write your reply to:

Draft