Linq [SOLVED]: Entity Framework and Linq syntax

Linq [SOLVED]: Entity Framework and Linq syntax

Home Forums Frameworks Linq Linq [SOLVED]: Entity Framework and Linq syntax

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #112050

    Cloudy Point
    Keymaster

    QuestionQuestion

    I’m trying to do a select where the final two letters of an id match whats in a string enumerable passed to the method but i;m getting the syntax wrong

    I’m trying to get it down to something along these lines

    setHeaders.SET_ID.Substring(setHeaders.SET_ID.Length - 2).ToUpper().Any(x => setIds.Contains(x.ToString()) )
    

    Heres the whole method at the moment

    var tupleResponse = (from setHeaders in _woEntities.SETHEADER
                                join setDetails in _woEntities.SETDETAIL on setHeaders.SET_ID equals setDetails.SET_ID
                                join workOrders in _woEntities.B1PERMIT on new
                                {
                                    setDetails.B1_PER_ID1,
                                    setDetails.B1_PER_ID2,
                                    setDetails.B1_PER_ID3
                                } equals new { workOrders.B1_PER_ID1, workOrders.B1_PER_ID2, workOrders.B1_PER_ID3 }
                                where setDetails.REC_STATUS == "A" && /*setHeaders.SET_ID.Substring(setHeaders.SET_ID.Length - 2).ToUpper().Any(x => setIds.Contains(x.ToString()) )*/
                                      (setHeaders.SET_ID.Substring(setHeaders.SET_ID.Length - 2).ToUpper() == "OS" ||
                                       setHeaders.SET_ID.Substring(setHeaders.SET_ID.Length - 2).ToUpper() == "TR")  &&
                                       workOrders.B1_APPL_STATUS.ToLower() == "open"
                                select new
                                {
    
                                    setHeaders.SET_ID,
                                    setHeaders.SET_TITLE,
                                    workOrders.B1_ALT_ID
                                }).AsEnumerable()
               .Select(x => Tuple.Create(x.SET_ID, x.SET_TITLE, x.B1_ALT_ID))
               .ToList();
    

    #112051

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    I think your latest testing would work better with .EndsWith

    setHeaders.SET_ID.ToUpper().EndsWith("TR")
    

    LINQ often can’t translate .Contains for a local collection, but if it could I don’t think you would need .Any or .ToString() (you know its a string).

    May not work

    setIds.Contains(setHeaders.SET_ID.Substring(setHeaders.SET_ID.Length - 2).ToUpper())
    

    If it doesn’t, you need to look into LINQKit and PredicateBuilder.

    Source: https://stackoverflow.com/questions/43991623/entity-framework-and-linq-syntax
    Author: NetMage
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.