Click here to Skip to main content
14,662,329 members
Home » Discussions » C#
   

C#

 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w1-Jul-20 0:15
Memberarnold_w1-Jul-20 0:15 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Luc Pattyn1-Jul-20 1:08
sitebuilderLuc Pattyn1-Jul-20 1:08 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w1-Jul-20 1:58
Memberarnold_w1-Jul-20 1:58 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Luc Pattyn1-Jul-20 2:08
sitebuilderLuc Pattyn1-Jul-20 2:08 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w1-Jul-20 2:12
Memberarnold_w1-Jul-20 2:12 
QuestionVS2017 publish project to .exe without manifest files Pin
Member 1487747430-Jun-20 10:35
MemberMember 1487747430-Jun-20 10:35 
AnswerRe: VS2017 publish project to .exe without manifest files Pin
Dave Kreskowiak30-Jun-20 11:15
mveDave Kreskowiak30-Jun-20 11:15 
QuestionOptimize Linq To SQL Query Pin
Kevin Marois30-Jun-20 8:21
professionalKevin Marois30-Jun-20 8:21 
This query is bit long, and it returns the correct data, but it takes about 4 minutes to run. I'd like to speed it up. The first part with the query runs pretty quick. The FOREACH loop part is the bottleneck.

Inside the FOREACH are 4 queries. They are all the same, except they pull from 4 different tables (JobHardwareVendors, JobLumberVendors, EquipmentHardwareVendors, TrussHardwareVendors). Those tables each have the same structure.

Those inner queries look like this:
/*********************************************************
    * Get the Hardware Vendors
    *********************************************************/
var vendors = (from v in db.JobHardwareVendors
                join c in db.Companies on v.VendorId equals c.Id
                where v.JobId == result.Id &&
                    !v.DeletedDT.HasValue
                select new CompanyHeaderEntity
                {
                    Id = c.Id,
                    CompanyName = c.CompanyName,
                    StatusId = c.StatusId
                }).Distinct().ToList();
result.HardwareCompanies.AddRange(vendors.Where(x => x.StatusId == companyStatuseActive.Id));

// Get the vendor's initials
foreach (var vendor in vendors)
{
    result.VendorInitials += GetInitials(vendor.CompanyName) + ",";
}
Here's the entire method
public async Task<List<TimelineReportEntity>> GetTimelineReportData(GenericReportArgsEntity reportArgs)
{
    var t = await Task.Factory.StartNew(() =>
    {
        List<TimelineReportEntity> results = null;

        using (var db = GetDataContext())
        {
            try
            {
                IQueryable<TimelineReportEntity> query = (from j in db.Jobs
                                                            join p in db.Projects on j.ProjectId equals p.Id
                                                            join c in db.Companies on p.CompanyId equals c.Id
                                                            join e in db.Employees on j.ForemanId equals e.Id into ep
                                                            from e in ep.DefaultIfEmpty()
                                                            join lc in db.Companies on j.LumberVendorId equals lc.Id into elc
                                                            from lc in elc.DefaultIfEmpty()
                                                            join hc in db.Companies on j.HardwareVendorId equals hc.Id into ehc
                                                            from hc in ehc.DefaultIfEmpty()
                                                            join tc in db.Companies on j.HardwareVendorId equals tc.Id into etc
                                                            from tc in etc.DefaultIfEmpty()
                                                            where !j.DeletedDT.HasValue
                                                            select new TimelineReportEntity
                                                            {
                                                                Id = j.Id,
                                                                JobId = j.JobNumber,
                                                                ProjectId = p.Id,
                                                                ProjectName = p.ProjectName,
                                                                CompanyId = c.Id,
                                                                CompanyName = c.CompanyName,
                                                                ForemanId = e.Id,
                                                                ForemanName = $"{e.FirstName} {e.LastName}",
                                                                Phase = j.Phase,
                                                                Quantity = j.Quantity,
                                                                Notes = j.Notes,
                                                            });

                // Create the predicate builder
                ExpressionStarter<TimelineReportEntity> predicate = null;

                if (reportArgs.IncludeAllProjects &&
                    reportArgs.IncludeAllCompanies &&
                    reportArgs.IncludeAllForemen)
                {
                    results = query.ToList();
                }
                else
                {
                    predicate = PredicateBuilder.New<TimelineReportEntity>();

                    if (!reportArgs.IncludeAllProjects)
                    {
                        foreach (var projectId in reportArgs.ProjectIds)
                        {
                            predicate = predicate.Or(p => p.ProjectId == projectId);
                        }
                    }

                    if (!reportArgs.IncludeAllCompanies)
                    {
                        foreach (var companyId in reportArgs.CompanyIds)
                        {
                            predicate = predicate.Or(p => p.CompanyId == companyId);
                        }
                    }

                    if (!reportArgs.IncludeAllForemen)
                    {
                        foreach (var foremanId in reportArgs.ForemenIds)
                        {
                            predicate = predicate.Or(p => p.ForemanId == foremanId);
                        }
                    }

                    results = query.Where(predicate).ToList();
                }

                var companyStatuses = GetLookups(Constants.LookupCategoryGenericFilters);
                var companyStatuseActive = companyStatuses.FirstOrDefault(x => x.AppCode == Constants.LookupCategoryGenericFiltersTypeActive);

                foreach (var result in results)
                {
                    result.ForemanInitials = result.ForemanName;

                    /*********************************************************
                        * Get the Hardware Vendors
                        *********************************************************/
                    var vendors = (from v in db.JobHardwareVendors
                                    join c in db.Companies on v.VendorId equals c.Id
                                    where v.JobId == result.Id &&
                                        !v.DeletedDT.HasValue
                                    select new CompanyHeaderEntity
                                    {
                                        Id = c.Id,
                                        CompanyName = c.CompanyName,
                                        StatusId = c.StatusId
                                    }).Distinct().ToList();
                    result.HardwareCompanies.AddRange(vendors.Where(x => x.StatusId == companyStatuseActive.Id));

                    // Get the vendor's initials
                    foreach (var vendor in vendors)
                    {
                        result.VendorInitials += GetInitials(vendor.CompanyName) + ",";
                    }

                    /*********************************************************
                        * Get the Lumber Vendors
                        *********************************************************/
                    vendors = (from v in db.JobLumberVendors
                                join c in db.Companies on v.VendorId equals c.Id
                                where v.JobId == result.Id &&
                                        !v.DeletedDT.HasValue
                                select new CompanyHeaderEntity
                                {
                                    Id = c.Id,
                                    CompanyName = c.CompanyName,
                                    StatusId = c.StatusId
                                }).Distinct().ToList();
                    result.LumberCompanies.AddRange(vendors.Where(x => x.StatusId == companyStatuseActive.Id));

                    // Get the vendor's initials
                    foreach (var vendor in vendors)
                    {
                        result.VendorInitials += GetInitials(vendor.CompanyName) + ",";
                    }

                    /*********************************************************
                        * Get the Equipment Vendors
                        *********************************************************/
                    vendors = (from v in db.JobEquipmentVendors
                                join c in db.Companies on v.VendorId equals c.Id
                                where v.JobId == result.Id &&
                                        !v.DeletedDT.HasValue
                                select new CompanyHeaderEntity
                                {
                                    Id = c.Id,
                                    CompanyName = c.CompanyName,
                                    StatusId = c.StatusId
                                }).Distinct().ToList();
                    result.EquipmentCompanies.AddRange(vendors.Where(x => x.StatusId == companyStatuseActive.Id));

                    // Get the vendor's initials
                    foreach (var vendor in vendors)
                    {
                        result.VendorInitials += GetInitials(vendor.CompanyName) + ",";
                    }

                    /*********************************************************
                        * Get the Truss Vendors
                        *********************************************************/
                    vendors = (from v in db.JobTrussVendors
                                join c in db.Companies on v.VendorId equals c.Id
                                where v.JobId == result.Id &&
                                        !v.DeletedDT.HasValue
                                select new CompanyHeaderEntity
                                {
                                    Id = c.Id,
                                    CompanyName = c.CompanyName,
                                    StatusId = c.StatusId
                                }).Distinct().ToList();
                    result.TrussCompanies.AddRange(vendors.Where(x => x.StatusId == companyStatuseActive.Id));

                    // Get the vendor's initials
                    foreach (var vendor in vendors)
                    {
                        result.VendorInitials += GetInitials(vendor.CompanyName) + ",";
                    }

                    // Remove trailing comma from vendor initials
                    if (!string.IsNullOrEmpty(result.VendorInitials))
                    {
                        result.VendorInitials = result.VendorInitials.TrimEnd(',');
                    }

                    /*********************************************************
                        * Get the latest Start Date
                        *********************************************************/
                    var startDateRevision = GetJobStartDateRevisions(result.Id).OrderByDescending(x => x.Revision).FirstOrDefault();

                    if (startDateRevision != null)
                    {
                        result.StartDate = startDateRevision.StartDate;
                        result.WeekGroup = result.StartDate.StartOfWeek(DayOfWeek.Monday);
                        result.FromDate = reportArgs.FromDate;
                        result.ToDate = reportArgs.ToDate;
                    }
                }

                results = results.Where(x => x.StartDate >= reportArgs.FromDate && x.StartDate <= reportArgs.ToDate).OrderBy(x => x.StartDate).ToList();
            }
            catch (Exception e)
            {
                throw;
            }
        }

        return results.ToList();
    });

    return t;
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

QuestionC# Pin
Member 1258896327-Jun-20 5:30
MemberMember 1258896327-Jun-20 5:30 
AnswerRe: C# Generate a password dictionary to log into API Pin
OriginalGriff27-Jun-20 5:49
mveOriginalGriff27-Jun-20 5:49 
GeneralRe: C# Generate a password dictionary to log into API Pin
Member 1258896327-Jun-20 5:59
MemberMember 1258896327-Jun-20 5:59 
GeneralRe: C# Generate a password dictionary to log into API Pin
OriginalGriff27-Jun-20 6:00
mveOriginalGriff27-Jun-20 6:00 
GeneralRe: C# Generate a password dictionary to log into API Pin
Dave Kreskowiak27-Jun-20 7:10
mveDave Kreskowiak27-Jun-20 7:10 
QuestionBest way to transform list and get top result from custom sort order? Pin
linalinea26-Jun-20 9:44
Memberlinalinea26-Jun-20 9:44 
AnswerRe: Best way to transform list and get top result from custom sort order? Pin
OriginalGriff26-Jun-20 11:28
mveOriginalGriff26-Jun-20 11:28 
GeneralRe: Best way to transform list and get top result from custom sort order? Pin
Richard Deeming29-Jun-20 0:21
mveRichard Deeming29-Jun-20 0:21 
AnswerRe: Best way to transform list and get top result from custom sort order? Pin
jsc4229-Jun-20 5:51
professionaljsc4229-Jun-20 5:51 
AnswerRe: Best way to transform list and get top result from custom sort order? Pin
linalinea3-Jul-20 9:59
Memberlinalinea3-Jul-20 9:59 
GeneralRe: Best way to transform list and get top result from custom sort order? Pin
OriginalGriff3-Jul-20 10:49
mveOriginalGriff3-Jul-20 10:49 
QuestionCustomizable keybinds C# Pin
Member 1486815919-Jun-20 14:28
MemberMember 1486815919-Jun-20 14:28 
AnswerRe: Customizable keybinds C# Pin
OriginalGriff19-Jun-20 20:10
mveOriginalGriff19-Jun-20 20:10 
QuestionC# windows application: how to enable and disable scrollbars in datagridview programatically? Pin
Rajasekaran Bose17-Jun-20 23:53
MemberRajasekaran Bose17-Jun-20 23:53 
AnswerRe: C# windows application: how to enable and disable scrollbars in datagridview programatically? Pin
Richard MacCutchan18-Jun-20 0:41
mveRichard MacCutchan18-Jun-20 0:41 
QuestionAnyone working with the new VS2019 .Net NI-VISA driver? Pin
SunshineDesign16-Jun-20 15:58
MemberSunshineDesign16-Jun-20 15:58 
AnswerRe: Anyone working with the new VS2019 .Net NI-VISA driver? Pin
Richard MacCutchan16-Jun-20 21:01
mveRichard MacCutchan16-Jun-20 21:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.