Reputation: 1250
I am trying to implement a search function where the results could be populated to a Gridview. I am using LINQ to query my db in c#.
The problem I am trying to figure out is, if the user want to search through multiple columns/fields with more than one search term, then how do I do that? So instance if I search a table with these 3 fields and I had some data like: > firstName | lastname | players# >>1. Michael | Jordan | 12 >>2. Michael | Jordan | 24 >>3. Michael | Jordan | 45 >>4. DeAndre| Jordan | 6 >>5. Jerome| Jordan | 44 >>6. Jordan| Sparks| 88
Now if I search for Jordan I get everything:
If I search Michael Jordan than I should get row #s 1,2,3 back.
If I search Jordan Sparks 88 than I should row # 6 back.
So my problem is I don't know where in the table the search term may be so I must search all columns/fields. In my current code, I have something where I go through each column/field name and use the Contains() then || ("or"), but it only works for 1 search term.
Is there an elegant and easy way to search and filter through a whole linq table so that the list is minimized? From there I will add that result to a datasource and then bind it.
@Sphinxxx I am using a typical gridview. It looks like this:
<asp:GridView ID="GridView" runat="server" AllowSorting="True" PagerStyle-Mode="NumericPages"
AutoGenerateColumns="false" Width="100%" CssClass="gridView" OnPageIndexChanging="GridView_PageIndexChanging"
AllowPaging="True" DataKeyNames="idPlayersList" OnRowCommand="GridView_RowCommand"
OnRowEditing="GridView_RowEditing" OnRowCancelingEdit="GridView_CancelEditRow"
OnRowUpdating="GridView_UpdateRow" OnRowDataBound="GridView_RowDataBound">
<RowStyle CssClass="rowStyle"></RowStyle>
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="PlayersNumber" HeaderText="Players Number" SortExpression="PlayersNumber" />
<asp:TemplateField HeaderText="Team" SortExpression="Team">
<EditItemTemplate>
<asp:DropDownList ID="ddlTeam" runat="server" CssClass="dropdown" AutoPostBack="True"
AppendDataBoundItems="true" DataTextField="TeamName" DataValueField="idTeam">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTeam" runat="server" Text='<%# Bind("TeamName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
My current search function looks something like this:
/**
* This method is for button search functionality
*
*/
protected void btnSearch_Click(object sender, EventArgs e)
{
// Call to Entity Model Framework
DBModel.DBEntities context = new DBModel.DBEntities();
string[] searchTerms = txtSearch.Text.Trim().Split('&');
//Prepare to build a "players" query:
IQueryable<DBModel.playersList> playersListQuery = context.playersLists;
foreach (var term in searchTerms)
{
//Refine our query, one search term at a time:
playersListQuery = playersListQuery.Where(p => p.isDeleted == false && (p.FirstName.Contains(term.Trim()) ||
p.LastName.Contains(term.Trim()) ||
p.PlayersNumber.Contains(term.Trim()) ||
p.Team.Name.Contains(term.Trim())));
}
//Now we have the complete query. Get the results from the database:
var filteredplayersList = playersListQuery.Select(s => new
{
idPlayersList = s.idPlayersList,
FirstName = s.FirstName,
LastName = s.LastName,
PlayersNumber = s.PlayersNumber,
TeamName = s.Team.Name
}).ToList();
GridView.DataSource = filteredplayersList; //Connecting query to the datasource Gridview
GridView.DataBind(); //Binding Gridview
}
Upvotes: 4
Views: 13828
Reputation: 814
If you just need to use the Contains condition for all the properties, what you can do is to encapsulated all the properties that you need into a single one, then you can apply the LINQ expression with fewer fields, it would look cleaner.
public class Player
{
public string SearchField { get => string.Format("{0}{1}{2}{3}",FirstName,LastName,PlayersNumber,Team); }
public string FirstName {get; set;}
public string LastName {get; set;}
public string PlayersNumber {get; set;}
public string Team {get; set;}
}
Even better I would recommend you to set the SearchField in the constructor when you create your object instance which will improve performance avoiding to create a substring for every invocation.
public string SearchField { get; private set; }
public Player(string firstName, string lastName, playersNumber, string team)
{
FirstName = firstName;
LastName = lastName;
PlayersNumber = playersNumber;
Team = team;
SearchField = string.Format("{0}{1}{2}{3}",firstName,lastName,playersNumber,team);
}
Then you just filter by SearchField property
foreach (var term in searchTerms)
{
var currentTerm = term.Trim();
playersQuery = playersQuery.Where(p => p.isDeleted == false &&
p.SearchField.Contains(currentTerm));
}
Now you have a LINQ expression with fewer fields which gets you the exact same result set.
One more thing I just saw that you mentioned you were working with LINQ to SQL query, you could apply the same approach by creating a Computed Column in your SQL table that concatenate the value from multiple columns, you can do this since SQL Server 2012.
Hopefully someone finds this solution helpful.
Regards!
Upvotes: 0
Reputation: 13057
Luckily, creating multiple ANDs (one for each search term) is easy in Linq2SQL - just keep adding .Where(..
to your query:
string[] searchTerms = "Jordan Sparks 88".Split(' ');
using (var dc = new MyDataContext())
{
//Using DataContext.Log is handy
//if we want to look at Linq2SQL's generated SQL:
dc.Log = new System.IO.StringWriter();
//Prepare to build a "players" query:
IQueryable<Player> playersQuery = dc.Players;
//Refine our query, one search term at a time:
foreach (var term in searchTerms)
{
//Create (and use) a local variable of the search term
//to avoid the "outer variable trap":
//http://stackoverflow.com/questions/3416758
//http://stackoverflow.com/questions/295593
var currentTerm = term.Trim();
playersQuery = playersQuery.Where(p => (p.isDeleted == false)
&&
(p.FirstName.Contains(currentTerm) ||
p.LastName.Contains(currentTerm) ||
p.PlayersNumber.Contains(currentTerm) ||
p.Team.Name.Contains(currentTerm))
);
}
//Now we have the complete query. Get the results from the database:
var filteredPlayers = playersQuery.Select(p => new
{
p.idPlayer,
p.FirstName,
p.LastName,
p.PlayersNumber,
TeamName = p.Team.Name
})
.ToArray();
//See if the generated SQL looked like it was supposed to:
var sql = dc.Log.ToString();
}
Upvotes: 6