Delete the value in the database if the file does not exist - c#

I'am scanning a folder like this
DirectoryInfo di = new DirectoryInfo(path);
var folders = di.GetDirectories().ToList().Select(d => d.Name);
var files = di.GetFiles();
I'am saving the filenames to the database and can get all the files from the database like this
var image = db.Images.ToList()
I want to delete the row from the database if the file does not exists
I'am trying something like this but it's not working
var myimages = db.Images.ToList();
foreach (var img in myimages) {
var fileExist = files.FirstOrDefault(x => x.Name.ToString().Equals(img));
if (fileExist == null)
{
Delete from database
}

If you have id for each row in your database, you can try this :
var myimages = db.Images.ToList();
string ids = "";
foreach (var img in myimages) {
var fileExist = files.FirstOrDefault(x => x.Name.ToString().Equals(img));
if (fileExist != null)
ids+=img.id.ToSting()+",";
}
if(ids=="")
return
ids = ids.Remove(ids.Length-1,1);
db.Database.ExecuteSqlCommand("delete from [Images] where id not in ("+ids+")");

should be your dbobject.yourtable.remove(img);

You can try this:
if(fileexist == null)
{
db.Images.Remove(img);
db.SaveChanges();
}

It is a simple question,Hope this works. Good luck
I think your Image class contains a name field if so do this way. otherwise just modify this. Just ask if you have doubt
var myimages = db.Images.ToList();
foreach (var img in myimages)
{
var fileExist = files.FirstOrDefault(x => x.Name.ToString().Equals(img.name));
if (fileExist == null)
{
db.Images.Remove(img);
db.SaveChanges();
}
}

Related

Downloading file from a variable thats passed from another class to Controller

I am trying to download files from my controller. I am receiving a variable from from a different class and need to download what is on that variable. I know I am probably doing a lot wrong right now. Any guidance would help a lot
Here is my class and I am returning the value allPaths
public class DBQueries
{
public List<string> GetSpecificYears(string[] response)
{
List<string> allPaths = new List<string>();
if (response != null)
{
using (var db = new ....())
{
foreach (var aYear in response)
{
// access a DbSet from DbContext here!
List<string> paths = db.ClientStatement_Inventory
.Where(x => x.statementYear == aYear)
.Select(y => y.statementPath).ToList();
allPaths.AddRange(paths);
}
}
}
return allPaths;
}
}
}
I am then passing allPaths variable to my controller by setting var allYears = getyears.GetSpecificYears()
Here is my Controller:
public ActionResult ExportFile(string[] years, string[] months, string[]radio, string[] acctNum)
{
ClientStatement_Inventory theStatementPath = new ClientStatement_Inventory();
var thePath = theStatementPath.statementPath;
DBQueries getyears = new DBQueries();
var allYears = getyears.GetSpecificYears(years);
var document = new ClientStatement_Inventory();
var cd = new System.Net.Mime.ContentDisposition
{
FileName = document.statementYear,
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(document.statementPath, document.statementYear);
How can I download these files using the allyears variable that is holding all the info?

How can I ensure rows are not loaded twice with EF / LINQ

I created code to load definitions from an external API. The code iterates through a list of words, looks up a definition for each and then I thought to use EF to insert these into my SQL Server database.
However if I run this twice it will load the same definitions the second time. Is there a way that I could make it so that EF does not add the row if it already exists?
public IHttpActionResult LoadDefinitions()
{
var words = db.Words
.AsNoTracking()
.ToList();
foreach (var word in words)
{
HttpResponse<string> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
.header("X-Mashape-Key", "xxxx")
.header("Accept", "application/json")
.asJson<string>();
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(response.Body);
var results = rootObject.results;
foreach (var result in results)
{
var definition = new WordDefinition()
{
WordId = word.WordId,
Definition = result.definition
};
db.WordDefinitions.Add(definition);
}
db.SaveChanges();
}
return Ok();
}
Also would appreciate if anyone has any suggestions as to how I could better implement this loading.
foreach (var result in results)
{
if(!(from d in db.WordDefinitions where d.Definition == result.definition select d).Any())
{
var definition = new WordDefinition()
{
WordId = word.WordId,
Definition = result.definition
};
db.WordDefinitions.Add(definition);
}
}
You can search for Definition value.
var wd = db.WordDefinition.FirstOrDefault(x => x.Definition == result.definition);
if(wd == null) {
var definition = new WordDefinition() {
WordId = word.WordId,
Definition = result.definition
};
db.WordDefinitions.Add(definition);
}
In this way you can get a WordDefinition that already have your value.
If you can also use WordId in the same way:
var wd = db.WordDefinition.FirstOrDefault(x => x.WordId == word.WordId);

Trying to access variable from outside foreach loop

The application I am building allows a user to upload a .csv file, which will ultimately fill in fields of an existing SQL table where the Ids match. First, I am using LinqToCsv and a foreach loop to import the .csv into a temporary table. Then I have another foreach loop where I am trying to loop the rows from the temporary table into an existing table where the Ids match.
Controller Action to complete this process:
[HttpPost]
public ActionResult UploadValidationTable(HttpPostedFileBase csvFile)
{
var inputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true
};
var cc = new CsvContext();
var filePath = uploadFile(csvFile.InputStream);
var model = cc.Read<Credit>(filePath, inputFileDescription);
try
{
var entity = new TestEntities();
var tc = new TemporaryCsvUpload();
foreach (var item in model)
{
tc.Id = item.Id;
tc.CreditInvoiceAmount = item.CreditInvoiceAmount;
tc.CreditInvoiceDate = item.CreditInvoiceDate;
tc.CreditInvoiceNumber = item.CreditInvoiceNumber;
tc.CreditDeniedDate = item.CreditDeniedDate;
tc.CreditDeniedReasonId = item.CreditDeniedReasonId;
tc.CreditDeniedNotes = item.CreditDeniedNotes;
entity.TemporaryCsvUploads.Add(tc);
}
var idMatches = entity.Authorizations.ToList().Where(x => x.Id == tc.Id);
foreach (var number in idMatches)
{
number.CreditInvoiceDate = tc.CreditInvoiceDate;
number.CreditInvoiceNumber = tc.CreditInvoiceNumber;
number.CreditInvoiceAmount = tc.CreditInvoiceAmount;
number.CreditDeniedDate = tc.CreditDeniedDate;
number.CreditDeniedReasonId = tc.CreditDeniedReasonId;
number.CreditDeniedNotes = tc.CreditDeniedNotes;
}
entity.SaveChanges();
entity.Database.ExecuteSqlCommand("TRUNCATE TABLE TemporaryCsvUpload");
TempData["Success"] = "Updated Successfully";
}
catch (LINQtoCSVException)
{
TempData["Error"] = "Upload Error: Ensure you have the correct header fields and that the file is of .csv format.";
}
return View("Upload");
}
The issue in the above code is that tc is inside the first loop, but the matches are defined after the loop with var idMatches = entity.Authorizations.ToList().Where(x => x.Id == tc.Id);, so I am only getting the last item of the first loop.
So I would need to put var idMatches = entity.Authorizations.ToList().Where(x => x.Id == tc.Id); in the first loop, but then I can't access it in the second. If I nest the second loop then it is way to slow. Is there any way I could put the above statement in the first loop and still access it. Or any other ideas to accomplish the same thing? Thanks!
Instead of using multiple loops, keep track of processed IDs as you go and then exclude any duplicates.
[HttpPost]
public ActionResult UploadValidationTable(HttpPostedFileBase csvFile)
{
var inputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true
};
var cc = new CsvContext();
var filePath = uploadFile(csvFile.InputStream);
var model = cc.Read<Credit>(filePath, inputFileDescription);
try
{
var entity = new TestEntities();
var tcIdFound = new HashSet<string>();
foreach (var item in model)
{
if (tcIdFound.Contains(item.Id))
{
continue;
}
var tc = new TemporaryCsvUpload();
tc.Id = item.Id;
tc.CreditInvoiceAmount = item.CreditInvoiceAmount;
tc.CreditInvoiceDate = item.CreditInvoiceDate;
tc.CreditInvoiceNumber = item.CreditInvoiceNumber;
tc.CreditDeniedDate = item.CreditDeniedDate;
tc.CreditDeniedReasonId = item.CreditDeniedReasonId;
tc.CreditDeniedNotes = item.CreditDeniedNotes;
entity.TemporaryCsvUploads.Add(tc);
}
entity.SaveChanges();
entity.Database.ExecuteSqlCommand("TRUNCATE TABLE TemporaryCsvUpload");
TempData["Success"] = "Updated Successfully";
}
catch (LINQtoCSVException)
{
TempData["Error"] = "Upload Error: Ensure you have the correct header fields and that the file is of .csv format.";
}
return View("Upload");
}
If you want to make sure you get the last value for any duplicate ids, then store each TemporaryCsvUpload record in a dictionary instead of using only a HashSet. Same basic idea though.
Declare idMatches before the first loop, but don't instantiate it or set its value to null. Then you'll be able to use it inside both loops. After moving the declaration before the first loop, you'll still end up having the values from the last iteration using a simple Where. You'll need to concatenate the already existing list with results for the current iteration.

Whats the best way to search files in a directory for multiple extensions and get the last write time according to filename

I am having a hard time mixing types with linq in the forloop. Basically i need to search a directory with a dbname, not knowing if the file will be .bak or .7z. If there are multiple files with the same dbname i need to get the one with extention .7z. If there are multiple files with same dbname and extention .7z I need to get the file with the last write time. This is what i have so far.
string [] files = Directory.GetFiles(directory, "*.*", SearchOption.TopDirectoryOnly);
foreach (var fileName in files)
{
var dbName = "Test";
var extention7 = ".7z";
var extentionBak = ".bak";
if (fileName.Contains(dbName) && (fileName.Contains(extention7) || fileName.Contains(extentionBak)))
{
Console.WriteLine(fileName);
}
}
I wouldn't create a LINQ only solution for this - it will be too hard to understand.
Here is what I would do:
string GetDatabaseFile(string folder, string dbName)
{
var files =
Directory.EnumerateFiles(folder, dbName + "*.*")
.Select(x => new { Path = x, Extension = Path.GetExtension(x) })
.Where(x => x.Extension == ".7z" || x.Extension == ".bak")
.ToArray();
if(files.Length == 0)
return null;
if(files.Length == 1)
return files[0].Path;
var zippedFiles = files.Where(x => x.Extension == ".7z").ToArray();
if(zippedFiles.Length == 1)
return zippedFiles[0].Path;
return zippedFiles.OrderByDescending(x => File.GetLastWriteTime(x.Path))
.First().Path;
}
Please note that this doesn't take into account the case where there are no .7z files but multiple .bak files for a DB. If this scenario can occur, you need to extend the method accordingly.
Get files in directory:
var sourceFilePaths = Directory.EnumerateFiles(sourceDirectory).Where(f => Path.GetExtension(f).ToLower() == ".exe" ||
Path.GetExtension(f).ToLower() == ".dll" ||
Path.GetExtension(f).ToLower() == ".config");
.
.
.
File compare:
var sourceFileInfo = new FileInfo(filePath);
var destinationFileInfo = new FileInfo(destinationFilePath);
var isNewer = sourceFileInfo.LastWriteTime.CompareTo(destinationFileInfo.LastWriteTime) > 0;
Instead of packing everything in one if condition you should handle all cases separate:
var dbName = "Test";
var extention7 = ".7z";
var extentionBak = ".bak";
foreach (var fileName in files)
{
if (!fileName.Contains(dbName)) continue; // wrong base name
if (File.GetExtension(filename) == extention7)
{
// handle this case:
// extract file date
// remember latest file
}
else if(File.GetExtension(filename) == extentionBak)
{
// handle this case
}
}

Find new file in two folders with a cross check

I am trying to sort two folders in to a patched folder, finding which file is new in the new folder and marking it as new, so i can transfer that file only. i dont care about dates or hash changes. just what file is in the new folder that is not in the old folder.
somehow the line
pf.NFile = !( oldPatch.FindAll(s => s.Equals(f)).Count() == 0);
is always returning false. is there something wrong with my logic of cross checking?
List<string> newPatch = DirectorySearch(_newFolder);
List<string> oldPatch = DirectorySearch(_oldFolder);
foreach (string f in newPatch)
{
string filename = Path.GetFileName(f);
string Dir = (Path.GetDirectoryName(f).Replace(_newFolder, "") + #"\");
PatchFile pf = new PatchFile();
pf.Dir = Dir;
pf.FName = filename;
pf.NFile = !( oldPatch.FindAll(s => s.Equals(f)).Count() == 0);
nPatch.Files.Add(pf);
}
foreach (string f in oldPatch)
{
string filename = Path.GetFileName(f);
string Dir = (Path.GetDirectoryName(f).Replace(_oldFolder, "") + #"\");
PatchFile pf = new PatchFile();
pf.Dir = Dir;
pf.FName = filename;
if (!nPatch.Files.Exists(item => item.Dir == pf.Dir &&
item.FName == pf.FName))
{
nPatch.removeFiles.Add(pf);
}
}
I don't have the classes you are using (like DirectorySearch and PatchFile), so i can't compile your code, but IMO the line _oldPatch.FindAll(... doesn't return anything because you are comparing the full path (c:\oldpatch\filea.txt is not c:\newpatch\filea.txt) and not the file name only. IMO your algorithm could be simplified, something like this pseudocode (using List.Contains instead of List.FindAll):
var _newFolder = "d:\\temp\\xml\\b";
var _oldFolder = "d:\\temp\\xml\\a";
List<FileInfo> missing = new List<FileInfo>();
List<FileInfo> nPatch = new List<FileInfo>();
List<FileInfo> newPatch = new DirectoryInfo(_newFolder).GetFiles().ToList();
List<FileInfo> oldPatch = new DirectoryInfo(_oldFolder).GetFiles().ToList();
// take all files in new patch
foreach (var f in newPatch)
{
nPatch.Add(f);
}
// search for hits in old patch
foreach (var f in oldPatch)
{
if (!nPatch.Select (p => p.Name.ToLower()).Contains(f.Name.ToLower()))
{
missing.Add(f);
}
}
// new files are in missing
One possible solution with less code would be to select the file names, put them into a list an use the predefined List.Except or if needed List.Intersect methods. This way a solution to which file is in A but not in B could be solved fast like this:
var locationA = "d:\\temp\\xml\\a";
var locationB = "d:\\temp\\xml\\b";
// takes file names from A and B and put them into lists
var filesInA = new DirectoryInfo(locationA).GetFiles().Select (n => n.Name).ToList();
var filesInB = new DirectoryInfo(locationB).GetFiles().Select (n => n.Name).ToList();
// Except retrieves all files that are in A but not in B
foreach (var file in filesInA.Except(filesInB).ToList())
{
Console.WriteLine(file);
}
I have 1.xml, 2.xml, 3.xml in A and 1.xml, 3.xml in B. The output is 2.xml - missing in B.

Categories

Resources