how to ignore first and last words using SOUNDEX mssql? - c#

I need to find all possible words using SOUNDEX.
For example:
I have the table (users) with one field (name):
Users have next view
|ID|Name |
|1 |Maria |
|2 |Ana Maria |
|3 |Maria Ana |
if I use Like, I have:
SELECT * FROM users WHERE name like '%Maria%'
As result I got
1.Maria
2.Ana Maria
3.Maria Ana
But if I use SOUNDEX
SELECT * FROM users WHERE SOUNDEX(name) = SOUNDEX('Maria')
I got the only two names (Maria and Maria Ana)
I need get result such as when I use like

Related

Linq get rows by enum values

Here is my enum values defined
public enum authaccess
{ read=0,create=1,update=2,delete=4}
As a Access Table looks like as below.
|id | tablename |columnname|permitted|
|----|-----------|----------|---------|
|1 | cms |header |3 |
|2 | cms |footer |2 |
|3 | cms |content |7 |
read access is permitted for all content, while 3 is for(create + update), and 7 for all rights.
As I need (x) button enabled in div where loggedInUser has delete authority
Using Linq, I used
new DbContext().tbl_access.where(k=>k.permitted > authaccess.delete){divid.class.add('close')}
but could not find a way to get a list of contents that can be changed by user ie 3 or (authaccess.create + authaccess.update)
[Clarification on edit]
The resultant table should be
|id | tablename |columnname|permitted|
|----|-----------|----------|---------|
|1 | cms |header |3 |
|2 | cms |footer |2 |
Because (header and footer) include create+update level access which is less than or equal to 3.
Thanks in advance for any help.
It looks like your authaccess has values for bit masking. For your requested create + update, a possible way to write your Linq is:
new DbContext().tbl_access
.Where(k => (k.permitted & (int)authaccess.create) > 0) &&
(k.permitted & (int)authaccess.update) > 0))
Do you want something like that;
var authAccessFilter = (int)authaccess.create + (int)authaccess.update;
var records = new DbContext().tbl_access.Where(k => k.permitted <= authAccessFilter).ToList();

Entity framework 3 table join with one right join

I have 3 tables:
strings
-----------
- string_id
- fallback_text
translations
----------------------
- translation_id
- string_id
- locale_id
- text
locales
---------------------
- locale_id
I want to achieve a resultset like this:
string_id | locale_id | text
----------------------------
1 | en_US | bread
1 | es_ES | pan
1 | fr_FR | NULL
There is no translation for "bread" in french, but I want it in the results.
In SQL would be somethink like:
SELECT strings.string_id, locales.locale_id, translations.text
FROM strings
JOIN translations on strings.string_id = translations.string_id
RIGHT JOIN locales on translations.locale_id = locales.locale_id
This SELECT doesn't resolve my problem. What I want is to list every string who hasn't a translation in each locale existing in locales table. I think this isn't going to happen in a SELECT statement.
Imagine we have:
locales table:
|locale_id|
-----------
|en_US |
|es_ES |
strings table:
|string_id|fallback_text|
-------------------------
|1 |bread |
|2 |water |
translations table:
|translation_id|string_id|locale_id|translation
-----------------------------------------------
|1 |1 |en_US |bread
|2 |2 |es_ES |agua
I would like to achieve this resultset:
string_id | locale_id | text
----------------------------
1 | en_US | bread
1 | es_ES | NULL
2 | en_US | NULL
2 | es_ES | agua
Thanks in advance.
The easiest way to accomplish this is simply use a SQL view.
Create an SQL view with you select statement, then create an entity to match that view. This entity will have 3 properties string_id, locale_id, text. Make sure that this entity defines (string_id and locale_id) as primary key. If you are using code first your mappings would be something like:
ToTable("MyViewName");
HasKey(x => new { x.string_id, x.locale_id });
var query = from s in db.strings
from t in db.translation
where s.string_id == t.string_id
select new {s.stringid, t.locale_id, t.text }

Inputing Value in sql after counting c#

I'm kind of new to this but I'll try to give as much detail as possible. This is my SQL table.
Customers
Customers_Id (PK) | First | Last | Address | Phone | Tech_Id (FK) |
-------------------+-------+-------+--------------+----------+--------------+
1 | Bob | Smith | 123 Fake St. | 3298492 | 1 |
2 | John | Man | 123 Noe St. | 2930482 | 1 |
3 | Tom | Lee | 123 Polk St. | 9308523 | 2 |
...
Tech
Tech_Id (PK) | First | Last | Phone | Customer_Count |
--------------+-------+-------+---------+----------------+
1 | Tim | Bo | 9384027 | |
2 | Andy | Wong | 9374927 | |
3 | Jack | Help | 2183847 | |
...
I'm trying to find the best way to count how many customer that each tech has either using SQL Query or C# coding. I was thinking of doing query with Count and then insert into the Customer_Count in Tech table.
I'm using visual studio 2012 and SQL is created locally in visual studio. Please help!
You can do it through SQL - using a GROUP BY clause to group the result by each tech. You can use the COUNT function to return the number of customers assigned to each tech. You can put this in an UPDATE statement to update the customer_count field in the tech table for each tech.
For example:
UPDATE t
SET t.customer_count = COUNT(c.customer_id)
FROM tech t
INNER JOIN customers c ON c.tech_id = t.tech_id
GROUP BY c.tech_id
Remove the Customer_Count column from the tech table. It's best not to make columns which duplicate available data unless there's a valid performance reason. If you want it displayed as such, then create a view:
SELECT t.tech_id,
t.first,
t.last,
t.phone,
(SELECT COUNT(c.customer_id)
FROM customers c
WHERE c.tech_id = t.tech_id) AS Customer_Count
FROM tech t
Now you can query it like a table, but you're not tasked with maintaining data which is always up to date through this view.
I prefre to using Sql ,but failed...Disappionted..
But I am sure that you can use Ado.net to do this job.
Here is C# demo code.
//query from db use C#
var allCus = new List<Customers>();
var gps = allCus.GroupBy(w => w.Tech_Id);
foreach(var gp in gps) {
var techId = gp.Key;
var cnt = gp.Count();
//update Tech set Customer_Count = cnt where Tech_Id = techId
}
Here is Sql version
update Tech set Customer_Count = (select IdAndCnt.cnt from (select Tech_Id,count (Tech_id) as cnt from Customers group by Tech_Id ) as IdAndCnt where Tech.Tech_Id = IdAndCnt.Tech_Id)
Holp it works.

Linq except return whole table

I have a table of items using linq to entities, say:
ID | Name | Attb1 | Attb2
1 | Apple | Green | Juicy
2 | Orange | Orange | sweet
etc
I have another list with just ID's in it.
Using Linq I want to return all fields from the item table except where the ID is in the list. i.e. if my list just has '1' in it I want to return 2 | orange | orange | sweet
You could try something like this
var result = table.Where(x => !list.Contains(x.id));
where I have assumed that table holds all the rows of your table and list contains the ids that you want to exclude.

Send Daily Emails via single nested loops function

I am creating a web page that will be run once a day to send email notices to supervisors. I want each supervisor to get a single email that contains data about all of their employees, of which might have multiple events.
I have some data like this:
supervisorEmail | employeeID | eventDate | eventDetails
===================================================================================================
george#blah.org | jones | 2014-03-18 | Watch a movie
george#blah.org | jones | 2014-03-20 | Convention registration
george#blah.org | smith | 2014-03-20 | Convention registration
george#blah.org | smith | 2014-03-20 | Convention registration
gayle#blah.org | nloya | 2014-03-13 | some other stuff
gayle#blah.org | nloya | 2014-03-25 | this and that
I start by retrieving all the above data and put into a DataTable.
Next I need to group by supervisor so each gets an email.
Within each email I need the rows filtered for that supervisor. I see how LINQ can be used for some of this. I'm lost at the moment for all of it. Could I get a shove in the right direction at least?
You can group the data by supervisorEmail:
var groups = data.GroupBy(d => d.supervisorEmail);
foreach(var g in groups)
{
SendEmail(g.Key, g); // g is an IEnumerable<some type> with the data for that supervisor
}
public void SendEmail(string supervisorEmail, IEnumerable<some type> data)
{
// send data to supervisor
}

Categories