I saved my file in the database, I want to get that file for sending mail If I wrote where the condition
public EMS_PROFILE_UPLOAD_MASTER GetHrUploadeProfile(string EnqId)
{
var x = from n in db.EMS_PROFILE_UPLOAD_MASTER
where n.ENQUIRY_CODE== EnqId
select n;
foreach(var fileData in x)
{
var _FilData = fileData.FILEDATA;
}
return x.FirstOrDefault();
}
I'm getting data but here I have multiple files in my database, how can I differentiate that file?
The fact that you have multiple files with the same ID - means you cannot use the ID as the selector. Inside your code example, it seems that there may be other meta-data from that fileData object - what is inside that to further filter or control your selection for the correct file?
Related
I have a table that store data from an uploaded excel sheet. Every time the user uploads a new list, it needs to check against the existing list if there's a duplicate value "A" before inserting a new row.
So far, this is what I have (as an example):
var originalData = await DataBase.ToListAsync();
List<Data> newData = new List<Data>();
/*... (getting data from file and add to newData list)...*/
/*** Below is where I have problem. What I try to achieve:
1 - Compare "ValueA" value from the newly uploaded data to the existing data, if they're the same then don't add to the database.
2 - Because the list that user upload may contains multiple rows with different datapoints and values but the same ValueA, it should only store one ValueA and ignore the rest (hence, the GroupBy)
***/
var list = newData.Where(n => originalData.Any(o => n.ValueA == o.ValueA))
.GroupBy(data => data.ValueA.ToLower())
.Select(data => data.First());
foreach(Data d in list){
DataBase.Add(d);
}
await DataBase.SaveChangesAsync();
When it uploads the files with multiple rows (with different data points) but the same value of ValueA, it adds the data with unique ValueA value (as expected). However, when I upload another file, it keeps adding even though it has duplicate value ValueA. The goal is to only add new data without the duplicate value of ValueA.
It looks to me like ValueA is a string - you'd do better to check for string equality by using the String.Equals function n.ValueA.Equals(o.ValueA) instead of n.ValueA == o.ValueA. Additionally, your filter is specifically getting rows from newData that do have rows with the same ValueA already in originalData; you should use !originalData.Any(... instead. All together, it should look like the following:
var list = newData.Where(n => !originalData.Any(o => n.ValueA.Equals(o.ValueA)))
.GroupBy(data => data.ValueA.ToLower())
.Select(data => data.First());
You can use the List.Contains method to check this.
foreach (var item in newData)
{
if (originalData.Contains(item)
{
return;
}
else
{
originalData.Add(item );
}
}
I have a DynamoDb table (named Fruit) with the following properties:
FruitId - string
CreatedDate - date
Type - number
Payload - blob
I also have a local list of strings List<string> fruitIds;.
I want to query the Fruit table and get only the Ids that have a corresponding record (i.e. exist) in the table.
What is a good way of doing that? Right now, I am looping over each Id in fruitIds and making a separate query to DyanmoDb to see if I get a record back, if I do, I then save that Id to another local variable called fruitIdsThatExistInDyanmoDb.
Is there a better way?
public IQueryable <fruits> GetAllfruitsIDs() {
return fruits.AsQueryable();
}
var data = GetAllfruitsIDs();
// Or u can use this :
public IEnumerable<fruits> GetAllfruitsIDs() {
return fruits.AsQueryable().ToList;
}
var data = GetAllfruitsIDs();
Using Linq, its very simple, just check if item's FruitId is in fruitIds:
var result = fruits.Where(f => fruitIds.Contains(f.FruitId));
to save their Ids in a new local variable as you said:
List<string> fruitIdsThatExistInDyanmoDb = fruits.Where(f => fruitIds.Contains(f.FruitId))
.Select(f=> f.FruitId).ToList();
Here is the what I am trying to achieve here:
Get a list of file names and IDs from the database
Search for those files on a network path
Store any IDs of the files not found
Search for that ID in a second database
Search for those files on a network path
List all ID's where files are not found on either location.
The issue I an encountering is trying to use the file names from results I have collected.
When running the code, the raw JSON data collected from the database gets displayed, however when trying to list just the file names I get nothing (not even an error)
Any ideas on how to fix this and list the file names in a way that would also let me search for them later? Or is there a better way to do this?
Also note: due to the version of SQL server being used i have to use FOR XML as FOR JSON is not compatible.
EDIT: Using code provided by Prany I am now able to output just the Audio File Name, but it I try and also output the sCallId I get a duplication Issue (see below output):
Getting Calls
2016\03\30\300320161614210106361-00000934412405.asf--84377-3668343241-514513
2016\03\30\300320161614210106361-00000934412405.asf--84385-3668343557-255773
2016\03\30\300320161614210106361-00000934412405.asf--84392-3668344445-516453
2016\03\30\300320161614210106361-00000934412405.asf--85000-3668749568-733799
2016\03\30\300320161614210106361-00000934412405.asf--85604-3668872399-722313
2016\03\30\300320161620220106362-00000934052048.asf--84377-3668343241-514513
2016\03\30\300320161620220106362-00000934052048.asf--84385-3668343557-255773
2016\03\30\300320161620220106362-00000934052048.asf--84392-3668344445-516453
2016\03\30\300320161620220106362-00000934052048.asf--85000-3668749568-733799
2016\03\30\300320161620220106362-00000934052048.asf--85604-3668872399-722313
2016\03\30\300320161634220106363-00000933211384.asf--84377-3668343241-514513
2016\03\30\300320161634220106363-00000933211384.asf--84385-3668343557-255773
2016\03\30\300320161634220106363-00000933211384.asf--84392-3668344445-516453
2016\03\30\300320161634220106363-00000933211384.asf--85000-3668749568-733799
2016\03\30\300320161634220106363-00000933211384.asf--85604-3668872399-722313
2016\04\04\040420160908190106389-00000527974488.asf--84377-3668343241-514513
2016\04\04\040420160908190106389-00000527974488.asf--84385-3668343557-255773
2016\04\04\040420160908190106389-00000527974488.asf--84392-3668344445-516453
2016\04\04\040420160908190106389-00000527974488.asf--85000-3668749568-733799
2016\04\04\040420160908190106389-00000527974488.asf--85604-3668872399-722313
2016\04\05\050420161913220106406-00000405271715.asf--84377-3668343241-514513
2016\04\05\050420161913220106406-00000405271715.asf--84385-3668343557-255773
2016\04\05\050420161913220106406-00000405271715.asf--84392-3668344445-516453
2016\04\05\050420161913220106406-00000405271715.asf--85000-3668749568-733799
2016\04\05\050420161913220106406-00000405271715.asf--85604-3668872399-722313
Below Is the code I am currently using to try and do this.
//Run the SQL and wrap the output in results tags to fix Multiple Root Elements error.
string liveXML = "<results>" + cmd2.ExecuteScalar().ToString() + "</results>";
//Create new XML Document
XmlDocument LiveDoc = new XmlDocument();
LiveDoc.LoadXml(liveXML);
//Conver XML to JSON
sjsonLive = JsonConvert.SerializeXmlNode(LiveDoc);
//Output RAW JSON
txtOut.AppendText("\r\n" + sjsonLive);
//Parse JSON into an Array
var files = JObject.Parse(sjsonLive);
//We want to run this values in a files seach, but for now let's print it to txtOut
foreach (var f in files.SelectTokens("$..calls..#audioFileName"))
foreach (var c in files.SelectTokens("$..calls..#sCallID"))
{
txtOut.AppendText("\r\n" + f.ToString() + " - " + c.ToString());
//Conduct File Search Here...
}
Example JSON Data:
{
"results": {
"calls": [{
"#audioFileName": "2016\\03\\30\\300320161614210106361-00000934412405.asf",
"#sCallID": "84377-3668343241-514513"
}, {
"#audioFileName": "2016\\03\\30\\300320161620220106362-00000934052048.asf",
"#sCallID": "84385-3668343557-255773"
}, {
"#audioFileName": "2016\\03\\30\\300320161634220106363-00000933211384.asf",
"#sCallID": "84392-3668344445-516453"
}, {
"#audioFileName": "2016\\04\\04\\040420160908190106389-00000527974488.asf",
"#sCallID": "85000-3668749568-733799"
}, {
"#audioFileName": "2016\\04\\05\\050420161913220106406-00000405271715.asf",
"#sCallID": "85604-3668872399-722313"
}
]
}
}
Edit:
You can use below token selector
files.SelectTokens("$..calls..#audioFileName")
edit 2:
var calls = files.SelectTokens("$..calls..#sCallID").ToList();
var audiofiles = files.SelectTokens("$..calls..#audioFileName").ToList();
for (int i = 0; i <= audiofiles.Count; i++)
{
//Conduct File search
if (true)
{
//access by index like audiofiles[i] and append to the query
}
else
{
//access calls by index like calls[i] and append to the query
}
}
I am currently working on a school project involving a large number of students where I have to insert a new student alphabetically and do a few other calculations. I am having trouble getting it so that it only adds the new student once. I have an if statement but it doesn't appear to be working properly.
`//this adds the new student
StreamWriter changeFile = new StreamWriter("Students.txt", true);
string newStudent = "(LIST (LIST 'Malachi 'Constant 'A ) '8128675309 'iwishihadjessesgirl#mail.usi.edu 4.0 )";
// this is where I am getting stumped
if (File.Exists(newStudent))
{
changeFile.Close();
}
else
{
changeFile.WriteLine(newStudent);
changeFile.Close();
}`
Whenever I run the code like this it will just add the new student every time I debug the program. How can I make it only add him one time?
File.Exists determines if the file at the given path exists (which, for the record, you should still be doing before trying to read/write to the file). You're trying to find out if the given line of text exists within a given file. That's a very different task.
You'll need to read through the lines in the file and compare them to your given text.
if(!File.ReadLines(filepath).Contains(newStudent))
{
//TODO: Append student to the file
}
File.Exists(string path) returns a bool that determines if a file exists at the specified path.
http://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx
string newStudent is not a file path, so it will always return false.
I think what you want is something like this: (this is by memory so it likely won't compile as is)
var file = File.Open("students.txt");
var fileContents = file.ReadToEnd();
if (!fileContents.Contains(newStudent))
{
file.WriteLine(newStudent);
}
file.Close();
First read the Existing file data into String variable and then check the given student data is available or not in the received file.if the given student data is not found then write the new student data into file otherwise,if already present then close the opened steream.
String StudentInfo = System.IO.File.ReadAllText("Students.txt");
StreamWriter changeFile = new StreamWriter("Students.txt", true);
string newStudent = "(LIST (LIST 'Malachi 'Constant 'A ) '8128675309 'iwishihadjessesgirl#mail.usi.edu 4.0 )";
// this is where I am getting stumped
if (StudentInfo.Contains(newStudent))
{
changeFile.Close();
}
else
{
changeFile.WriteLine(newStudent);
changeFile.Close();
}
Using the query function of entity collection in C# and it takes a long time to load the related records back from SQL Server 2008. Is there any fast way to do this? This is the query function I use:
public void SearchProducts()
{
//Filter by search string array(searchArray)
List<string> prodId = new List<string>();
foreach (string src in searchArray)
{
StoreProductCollection prod = new StoreProductCollection();
prod.Query.Where(prod.Query.StptName.ToLower() == src.ToLower() && prod.Query.StptDeleted.IsNull());
prod.Query.Select(prod.Query.StptName, prod.Query.StptPrice, prod.Query.StptImage, prod.Query.StptStoreProductID);
// prod.Query.es.Top = 4;
prod.Query.Load();
if (prod.Count > 0)
{
foreach (StoreProduct stpt in prod)
{
if (!prodId.Contains(stpt.StptStoreProductID.ToString().Trim()))
{
prodId.Add(stpt.StptStoreProductID.ToString().Trim());
productObjectsList.Add(stpt);
}
}
}
}
You're hitting the database once per searchArray item, this is very wrong.
You might get better performance like this (have no way of testing it, give it a shot):
public void SearchProducts()
{
//Filter by search string array(searchArray)
List<string> prodId = new List<string>();
StoreProductCollection prod = new StoreProductCollection();
// Notice that your foreach() is gone
// replace this
// prod.Query.Where(prod.Query.StptName.ToLower() == src.ToLower() && prod.Query.StptDeleted.IsNull());
// with this (or something similar: point is, you should call .Load() exactly once)
prod.Query.where(prod.Query.StptDeleted.IsNull() && src.Any(srcArrayString => prod.Query.StptName.ToLower()==srcArrayString.ToLower());
prod.Query.Select(prod.Query.StptName, prod.Query.StptPrice, prod.Query.StptImage, prod.Query.StptStoreProductID);
// prod.Query.es.Top = 4;
prod.Query.Load();
// ... rest of your code follows.
}
Given List<string> searchArray containing lowered words :
public void SearchProducts()
{
//Filter by search string array(searchArray)
List<string> prodId = new List<string>();
StoreProductCollection prod = new StoreProductCollection();
prod.Query.Where(searchArray.Contains(prod.Query.StptName.ToLower()) && prod.Query.StptDeleted.IsNull());
prod.Query.Select(prod.Query.StptName, prod.Query.StptPrice, prod.Query.StptImage, prod.Query.StptStoreProductID);
// prod.Query.es.Top = 4;
prod.Query.Load();
if (prod.Count > 0)
{
foreach (StoreProduct stpt in prod)
{
if (!prodId.Contains(stpt.StptStoreProductID.ToString().Trim()))
{
prodId.Add(stpt.StptStoreProductID.ToString().Trim());
productObjectsList.Add(stpt);
}
}
}
}
This way you have only one query for all words.
First of all, put an index on StptName column.
Second, if you need even better performance, write a Stored Procedure in SQL, to do your querying, and map it with Entity Framework.
Let me know if you need explanation on how to do any of the above.
A couple more micro-optimizations you can do if you don't want to write a Stored Procedure:
Write src.ToLower() in a temporary varaible, and than compare prod.Query.StptName.ToLower() to it.
By default, SQL Server queries are case insensitive, so check if that's the case, and if so, you can get rid of the ToLower altogether. You can change case sensitivity through Collation.
EDIT:
To create an Index:
Open the table designer in SQL Server Managment Studio.
Right click anywhere and select Indexes/Keys.
Click Add.
Under Columns add StptName.
Under Is Unique specify whether StptName is unique or not.
Under type select "index".
That's all!
As for mapping stored procedures - here's a nice tutorial:
http://www.robbagby.com/entity-framework/entity-framework-modeling-select-stored-procedures/
(You can jump straight to the "Map in the Select Stored Procedure" Section).