简体   繁体   中英

how to search record from single table with multiple parameters using LINQ?

I am trying to search record(s) from table by appying multiple search parameters. as per below snap. 在此处输入图片说明

here by using various parameters as per above snap i want to filter the records. here user could enter any combination of parameter(s) to search record. i tried something like below code hich works for single condition but fails for combination of any search paramets.

    public List<students> SearchStudents(students search)
            {
     var result = new List<students>();
     var records= from stud in db.students
                                    where stud.enrollmentNumber== search.enrollmentNumber
                                    || stud.enrollmentDate==search.enrollmenttDate
                                    || stud.enrollmentType==search.enrollmentType
                                    || stud.className==search.className
                                    select new Search()
                                    {
                                        enrollmentNumber= stud.enrollmentNumber,
                                        enrollmentDate = stud.enrollmentDate,
                                        enrollmentType = stud.enrollmentType,                                    
                                        Name = stud.Name,
                                        className=stud.className,                                                                      
                                        Description = stud.Description
                                    };
           result = records.ToList();
return result;
    }

but this is not working properly. means it returns same result whatever parameters I pass.

Like in the table i ahve 20 records and the enrollment number is the unique value field in DB so here when i am passing enrollment number thats like "2018-0001" it returns all records when it should return only single reocrd. can someone guide me with this?

Without further explanation in your question about how this isn't working, the best we can do is guess. However, one very plausible reason for this is because you're including parameters you don't want to be filtering on.

Because you're using OR s in your statement, if any of those other properties are defaulted in the database, you're going to be returning those records. What you need to be doing is conditionally including your pieces of the WHERE clauses for only the properties that you want to search on. Unfortunately, that is not possible with the "SQL syntax" version of LINQ, so you will need to convert your query to that. (Good news: It's slightly more performant as well as it usually has to convert the SQL to the method syntax.)

Because of deferred execution , your query will not be sent to the database until you call a .ToList() or something to actually start processing the results. This allows you to chain method calls together, even if they are completely different C# statements. This is what you'll want to do:

public List<students> SearchStudents(students search)
{
    var query = db.students;

    if (!string.IsNullOrWhiteSpace(search.enrollmentNumber))
    {
        query = query.Where(s => s.enrollmentNumber == search.enrollmentNumber);
    }

    if (search.enrollmentDate != DateTime.MinValue)
    {
        query = query.Where(s => s.enrollmentDate == search.enrollmentDate);
    }

    if (!string.IsNullOrWhiteSpace(search.enrollmentType))
    {
        query = query.Where(s => s.enrollmentType == search.enrollmentType);
    }

    if (!string.IsNullOrWhiteSpace(search.className))
    {
        query = query.Where(s => s.className == search.className);
    }

    return query.Select(stud => new Search
                                {
                                    enrollmentNumber= stud.enrollmentNumber,
                                    enrollmentDate = stud.enrollmentDate,
                                    enrollmentType = stud.enrollmentType,                                    
                                    Name = stud.Name,
                                    className=stud.className,                                                                      
                                    Description = stud.Description
                                })
                .ToList();
}

You may need to adjust the if statements in there to accommodate different data types than what is intuitive from the names, but this will only add the filter if a value has been provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM