MiziaQ
MiziaQ

Reputation: 21

C# LINQ Help with Errors

Below is an extract from a LINQ statement which is throwing up the following errors. Is there a way to reference ID (ID = af.AgileFactorID) like I am doing below?....psf.AgileFactorID == tagSummary.ID ? Thanks in advance for your help!

select new {
    ID = af.AgileFactorID,
    Total = psf.Count()
};

1) The name af does not exist in the current context, 2) the name psf does not exist in the current context

var tagCloud = from psf in tagSummary where psf.AgileFactorID == tagSummary.ID

1) Error 62 'AnonymousType#1' does not contain a definition for 'AgileFactorID' and no extension method 'AgileFactorID' accepting a first argument of type 'AnonymousType#1' could be found (are you missing a using directive or an assembly reference?)

2) Error 63 'System.Collections.Generic.IEnumerable' does not contain a definition for 'ID' and no extension method 'ID' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

Here is the full query:

private void BindTagCloud()
{



var tagSummary = from af in db.AgileFactors
           join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
           join s in db.Stories on psf.StoryID equals psf.StoryID
           join pim in db.ProjectIterationMembers on pim.ProjectIterationMemberID equals s.ProjectIterationMemberID
           join i db.Iteration on ...
           join p db.Project on ....
           where p.ProjectID == proj_id &&
                 p.ProjectID == i.ProjectID &&
                 i.ProjectIterationID == pim.ProjectIterationID &&
                 pim.ProjectIterationMemberID == s.ProjectIterationMemberID &&
                 s.StoryID == psf.StoryID &&
                 psf.AgileFactorID == af.AgileFactorID
                 group af by af.Name into tagGroup

                 select new
                 {

                    ID = af.AgileFactorID,
                    Total = psf.Count() 

                 };


 var tagCloud = from psf in tagSummary
         where psf.AgileFactorID == tagSummary.ID
 select new
 {

 Name = psf.Name,
 ID = psf.AgileFactionID,
 Count = psf.Count(),

 weight = Count / tagSummary.Total * 100

};


ListView1.DataSource = tagCloud; 
ListView1.DataBind();

}

Upvotes: 2

Views: 715

Answers (2)

sgmoore
sgmoore

Reputation: 16077

@MiziaQ,

I would suspect that the lack of answers may be because no-one has any idea where to start. There would appear to be so many things wrong with this that I hardly know where to begin. I also have no idea what you are trying to achieve.

But some obvious errors

Your attempt to reference af.AgileFactorID in your query would work IF you did not have the section

 group af by af.Name into tagGroup

However the group function means this is no longer available. If you need access AgileFactorID , would you need to include it in the grouping via

group af by new { af.Name, af.AgileFactorID} into tagGroup

This should allow you to access the value of AgileFactorID, but only by referring to it like

select new { AgileFactorID = tagGroup.Key.AgileFactorID} 

Similarly, because of the grouping, psf is no longer in scope. (This also is ignoring the fact that psf.Count() does not make sense).

The final query is also confusing. The select on the first query would indicate that it should (if it worked) provide a query returning two fields namely ID and Total. It does not return anything called AgileFactorID or Name.

I am assuming from the questions, that you are learning LINQ, so you might find some of the resources mentioned in this question helpful.

Upvotes: 0

Enigmativity
Enigmativity

Reputation: 117124

Your "full query" can't be correct as it has a number of syntax errors and missing parts.

Nevertheless, the best that I can reconstruct it is this:

var tagSummary =
    from af in db.AgileFactors
    join psf in db.ProjectStoryFactors
        on af.AgileFactorID equals psf.AgileFactorID
    join s in db.Stories on psf.StoryID equals s.StoryID
    join pim in db.ProjectIterationMembers
        on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
    join i in db.Iteration
        on pim.ProjectIterationID equals i.ProjectIterationID
    join p in db.Project on i.ProjectID equals p.ProjectID
    where p.ProjectID == proj_id
    group af by new { af.Name, af.AgileFactorID } into tagGroup
    select new
    {
        ID = tagGroup.Key.AgileFactorID,
        Name = tagGroup.Key.Name,
        Count = tagGroup.Count()
    };


var tagCloud =
    from t in tagSummary
    join psf in db.ProjectStoryFactors
        on t.ID equals psf.AgileFactorID into psfs
    let Count = psfs.Count()
    let Total = tagSummary.Count()
    select new
    {
        t.Name,
        t.ID,
        Count,
        Weight = (double)Count / Total * 100,
    };

How does this query work for you? Is it close?

Upvotes: 1

Related Questions