Using LINQ to determine if a value exists in a DB table - sql

I have a SQL table which has a simple string field that may contain a single value (like 'ABC' for example) or it may contain a list delimited by the pipe ('|') character (like 'ABC|EDF|GHI' for example).
If I have a SharePoint list and want to cross check to make sure that all of the values in my SP list exist in the SQL table, I can easily do it assuming the SQL field only has a single value:
var listitems = from SPListItem item in spList.Items
where item[myfieldname].Equals(valueImChecking)
select item;
How do I use a similar approach to check the same SQL field assuming it contains list of value delimited by '|'? If this field's value is 'ABC|EDF|GHI' and I'm checking if it contains 'GHI' for example, how do I build the query in LINQ?
TIA,
-Tony.

var listitems = from SPListItem item in spList.Items
where item[myfieldname].ToString().Contains(valueImChecking)
select item;
Contains method on string should help.

I would suggest to split the values and write them to a separate table. I don't think it's a good design to store multiple values in the same field if you want to lookup them later.
If you don't want to change your database you can use shahkalpesh's answer. If however some values may contain other values (like WXYZ contains XYZ) this will only give you a list of possible candidates. You need to check the result in-memory:
// this filters for possible candidates
var candidates = from item in spList.Items
where item[myfieldname].ToString().Contains(valueImChecking)
select item;
// this filters the result using ling-to-objects
var listItems = from candidate in candidates.AsEnumerable()
where candidate[myfieldname].ToString().Split('|').Contains(valueImChecking)
select candidate;
Using AsEnumerable switches to Linq-To-Objects. This is required because LINQ to SQL does not support Split.
You can do some pre-filtering using the first query (which was taken from shahkalpesh's answer).
If your Items table is small it might also be a good idea to load everything into memory and do the check with ling-to-objects (using only the second query).

Related

How to search a text contains or not in particular column that contain Json object using linq

I have a table that contains the column "FEEDDATA". In FEEDDATA column the values stored in json object. Now i want make a serach and fetch the data where "AuthorFirstName" key in FEEDDATA column have value "Lalit". I need to implement it in LINQ.
e.g f.FeedData.AuthorFirstName.contains("%Lal%")
In short: I need to implement search on the feedata column and fetch only those records in which key AuthorFirstName contains text "lal" .
Please help.
Using general LINQ to SQL, you can use the Contain method to do a LIKE SQL search and find matching records:
var ans = db.Where(r => r.FEEDDATA.Contains("\"AuthorFirstName\":\"Lalit\""));
Assuming the type of FEEDDATA is actually e.g. varchar and not a database specific special JSON type.

selecting a column values from a Table using LINQ (Entity Framework)

Im trying to display a dropdown with values from table 'Events'. I have created a Model.edmx which has the structure of the database. Now i have to just write LINQ code to display one column values. I am kind of new to LINQ.
Dim Events=" LINQ select statement part???"
ddlEvent.DataSource = Events
ddlEvent.DataBind()
ddlEvent.Items.Insert(0, New ListItem("-Type-", ""))
DropDownLists work with 2 fields:
the DataTextField represents the field holding the text of the options that will be displayed to the user
the DataValueField represents the field hoding the value associated with each option
So in your case, you could get it to work with something like:
Dim events As List(Of Event) = yourDbContext.Events.ToList()
ddlEvent.DataSource = events
ddlEvent.DataTextField = "NameOfThePropertyYouWantDisplayed"
ddlEvent.DataValueField = "NameOfThePropertyYouWantAsValue"
ddlEvent.DataBind()
Note, however, that the query will retrieve every property from the database. This can hurt performance if your Event class has a lot of properties.
To avoid this, you could use the LINQ Select operator, used for projection. This could be used to retrieve only the necesseray data from the database. You can have a look on the MSDN here
Context.Events.ToList();
Where, Context is your EF db context. Update your filter condition in where clause
ddlEvent.DataSource = from x in Context.Events select x.column;
here 'x' is a variable and 'column' in 'x.column' is required column you want to fetch.
Where, Context is your EF db context.

Pulling field names from SQL query

Is there a way to retrieve the field names from an actual query, similar to how you can retrieve field names from a table using the INFORMATION_SCHEMA? In essence, I'm wanting to accept an entire query as a parameter, and be able to build an empty table with the field names from said query.
Right now, I'm using a STUFF() function to replace the SELECT with SELECT TOP 1 and replacing the FROM with a INTO tablename FROM, and then truncating tablename. It works, but I need this to be able to handle complex queries where the first occurrence of FROM might not be the one I need (in case the user is using a subquery for a field, for example).

Remove a string inside a cell in MS Access

I want to delete a certain string in a cell in MS Access, whether in a column or in the whole table. I know that I can do this using plain old Find and Replace but in this case it is not economical for my thousand-row table.
For example,
remove all Unknown values from all columns.
remove the string "dollars" from the values in column price ie. if the cell contains "34 dollars", it will just be "34".
Can this be done in SQL and how?
Assuming your query will run within an Access session, the second goal is easy. You can Replace dollars with a zero-length string.
UPDATE YourTable
SET price = Replace(price, 'dollars', '');
You could use the same strategy for the first goal, but may decide it makes more sense to examine the datatypes of the table's fields and only UPDATE those which are text or memo.

query a table not in normal 3rd form

Hi I have a table which was designed by a lazy developer who did not created it in 3rd normal form. He saved the arrays in the table instead of using MM relation . And the application is running so I can not change the database schema.
I need to query the table like this:
SELECT * FROM myTable
WHERE usergroup = 20
where usergroup field contains data like this : 17,19,20 or it could be also only 20 or only 19.
I could search with like:
SELECT * FROM myTable
WHERE usergroup LIKE 20
but in this case it would match also for field which contain 200 e.g.
Anybody any idea?
thanx
Fix the bad database design.
A short-term fix is to add a related table for the correct structure. Add a trigger to parse the info in the old field to the related table on insert and update. Then write a script to [parse out existing data. Now you can porperly query but you haven't broken any of the old code. THen you can search for the old code and fix. Once you have done that then just change how code is inserted or udated inthe orginal table to add the new table and drop the old column.
Write a table-valued user-defined function (UDF in SQL Server, I am sure it will have a different name in other RDBMS) to parse the values of the column containing the list which is stored as a string. For each item in the comma-delimited list, your function should return a row in the table result. When you are using a query like this, query against the results returned from the UDF.
Write a function to convert a comma delimited list to a table. Should be pretty simple. Then you can use IN().