locked
Looking up file extension C# RRS feed

  • Question

  • User381476053 posted

     Hello,

    I converted a VB.Net file to C# and I'm getting issues with one line of code (in bold) that the error states: 

    Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

    Not sure what to use in the place of .Extension? Code is:

    -----------------------------------------------------------------

    public void PrintDocs(string basketId, string shopperId, string printerName)
    {
    try
    {
    _ixBase = new ImagXpress();
    _printPro = new PrintPro();
    _pdfRenderOpt = new Accusoft.PdfXpressSdk.RenderOptions
    {
    RenderAnnotations = true,
    ResolutionX = 300,
    ResolutionY = 300
    };
    _printer = Printer.SelectPrinter(_printPro, printerName);
    _pdfExpress = new Accusoft.PdfXpressSdk.PdfXpress();
    _pdfExpress.Initialize();
    dynamic dirInfo = new DirectoryInfo(ImgPath + basketId + "_" + shopperId);
    FileInfo[] fileInfo = dirInfo.GetFiles().Where(x => x.Extension == ".tif" | x.Extension == ".pdf").ToArray();
    //Loop through each document
    foreach (var tmpFile in fileInfo)
    {
    var ext = tmpFile.Extension.ToLower();
    var fileName = tmpFile.FullName;
    var pgCount = ImageX.NumPages(_ixBase, fileName);
    for (var i = 1; i <= pgCount; i++)
    {
    _printJob = new PrintJob(_printer);
    ImageX ixTemp;
    if (ext == ".tif")
    {
    ixTemp = ImageX.FromFile(_ixBase, fileName, i);
    }
    else
    {
    _pdfExpress.Documents.Add(fileName);
    var pdfBitmap = _pdfExpress.Documents[i - 1].RenderPageToBitmap(i - 1, _pdfRenderOpt);
    ixTemp = ImageX.FromBitmap(_ixBase, pdfBitmap);
    pdfBitmap.Dispose();
    }

    var dib = ixTemp.ToHdib(false);
    _printJob.PrintDib(dib, 0, 0, ixTemp.ImageXData.Width, ixTemp.ImageXData.Height, _printJob.LeftMargin, _printJob.TopMargin, _printJob.PrintWidth, _printJob.PrintHeight, true);

    if (_pdfExpress.Documents.Count > 0)
    _pdfExpress.Documents.Clear();
    _printJob.Finish();
    ixTemp.Dispose();
    _printJob.Dispose();
    _printJob = null;
    }
    }
    _pdfExpress.Dispose();
    _printer.Dispose();
    _printPro.Dispose();
    _ixBase.Dispose();
    }
    catch (Exception ex)
    {
    if (_printJob != null)
    _printJob.Kill();
    throw;
    }
    }

    Tuesday, June 17, 2014 2:32 PM

Answers

  • User-1360095595 posted

    Use two | instead of just one. So x.blah == blah || x.blah2 == blah2

    Edit: also use DirectoryInfo instead of dynamic to declare dirInfo.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 17, 2014 2:50 PM

All replies

  • User-1360095595 posted

    Use two | instead of just one. So x.blah == blah || x.blah2 == blah2

    Edit: also use DirectoryInfo instead of dynamic to declare dirInfo.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 17, 2014 2:50 PM
  • User281315223 posted

    If you needed to check the extensions of each of your appropriate files, you could use the Path.GetExtension() method as seen below :

    FileInfo[] files = YourDirectory.GetFiles().Where(x => Path.GetExtension(x) == ".tif" || Path.GetExtension(x) == ".pdf");

    or using the String.EndsWith() method :

    FileInfo[] files = YourDirectory.GetFiles().Where(x => x.EndsWith(".tif") || x.EndsWith(".pdf"));

    Tuesday, June 17, 2014 3:08 PM