Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:07:47+00:00 2026-06-07T06:07:47+00:00

I am trying to select some records using LINQ for Entities (EF4 Code First).

  • 0

I am trying to select some records using LINQ for Entities (EF4 Code First).

I have a table called Monitoring with a field called AnimalType which has values such as

  • “Lion,Tiger,Goat”
  • “Snake,Lion,Horse”
  • “Rattlesnake”
  • “Mountain Lion”

I want to pass in some values in a string array (animalValues) and have the rows returned from the Monitorings table where one or more values in the field AnimalType match the one or more values from the animalValues. The following code ALMOST works as I wanted but I’ve discovered a major flaw with the approach I’ve taken.

public IQueryable<Monitoring> GetMonitoringList(string[] animalValues)
    {
        var result = from m in db.Monitorings
                     where animalValues.Any(c => m.AnimalType.Contains(c))
                     select m;
        return result;
    }

To explain the problem, if I pass in animalValues = { “Lion”, “Tiger” } I find that three rows are selected due to the fact that the 4th record “Mountain Lion” contains the word “Lion” which it regards as a match.

This isn’t what I wanted to happen. I need “Lion” to only match “Lion” and not “Mountain Lion”.

Another example is if I pass in “Snake” I get rows which include “Rattlesnake”. I’m hoping somebody has a better bit of LINQ code that will allow for matches that match the exact comma delimited value and not just a part of it as in “Snake” matching “Rattlesnake”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:07:48+00:00Added an answer on June 7, 2026 at 6:07 am

    Because the data in your field is comma delimited you really need to break those entries up individually. Since SQL doesn’t really support a way to split strings, the option that I’ve come up with is to execute two queries.

    The first query uses the code you started with to at least get you in the ballpark and minimize the amount of data you’re retrieving. It converts it to a List<> to actually execute the query and bring the results into memory which will allow access to more extension methods like Split().

    The second query uses the subset of data in memory and joins it with your database table to then pull out the exact matches:

    public IQueryable<Monitoring> GetMonitoringList(string[] animalValues)
    {
        // execute a query that is greedy in its matches, but at least
        // it's still only a subset of data. The ToList()
        // brings the data into memory, so to speak
        var subsetData = (from m in db.Monitorings
                 where animalValues.Any(c => m.AnimalType.Contains(c))
                 select m).ToList();
    
        // given that subset of data in the List<>, join it against the DB again
        // and get the exact matches this time
        var result = from data in subsetData
                join m in db.Monitorings on data.ID equals m.ID
                where data.AnimalType.Split(',').Intersect(animalValues).Any ()
                select m;        
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ran into this error message while trying to select some records off a table.
I need some help with SQL Query. I am trying to select all records
I'm trying to find some records based on the tags they have. In order
I am trying to fetch records in MySQL using a simple used submitted field.
I'm trying to back up some records to a separate table in the same
I´m trying to update a db record using linq to sql First i query
I am trying to bring some data from Dynamics CRM 2011 using LINQ. The
i am trying to add up some records from a table, however, it isn't
I'm trying to select some integer values in MySQL. Several of the values are
Can anyone help. Im trying to select some elements on a form but as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.