Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

Issues trying to access data with LINQ

$
0
0

Ive got the following code that het some info from an Online CRM:

            var context = new XrmServiceContext();
        var contacts1 =
            (
                from c in context.ContactSet
                join m in context.py3_membershipSet on c.ContactId equals m.py3_Member.Id
                where m.statuscode.Value == 1

                orderby c.LastName
                select new
                {
                    ContactId = c.ContactId,
                    FirstName = c.FirstName,
                    LastName = c.LastName,
                    BranchCode = c.py3_BranchArea,
                    Branch = (c.FormattedValues != null && c.FormattedValues.Contains("py3_brancharea") ? c.FormattedValues["py3_brancharea"] : "N/a"),
                    JobTitle = c.JobTitle,
                    Organisation = (c.ParentCustomerId != null ? c.ParentCustomerId.Name : "N/a"),
                    joinedAsCode = c.py3_SOLACEMemberJoinedAs,
                    JoinedAs = (c.FormattedValues != null && c.FormattedValues.Contains("py3_solacememberjoinedas") ? c.FormattedValues["py3_solacememberjoinedas"] : "N/a"),
                    Expertise = (c.py3_SOLACEMemberAreasofExpertise != null && c.py3_SOLACEMemberAreasofExpertise.Trim() != String.Empty ? c.py3_SOLACEMemberAreasofExpertise : "N/a")
                }
            );

I then bind this to a datalist as an array, which all works fine.

However I want to be able to limit the results to a value selected from a dropdownlist, and expected the following to work:

            var context = new XrmServiceContext();
        var contacts1 =
            (
                from c in context.ContactSet
                join m in context.py3_membershipSet on c.ContactId equals m.py3_Member.Id
                where m.statuscode.Value == 1 &&
                    c.FormattedValues["py3_brancharea"] == ddlBranchTags.SelectedItem.Value


                orderby c.LastName
                select new
                {
                    ContactId = c.ContactId,
                    FirstName = c.FirstName,
                    LastName = c.LastName,
                    BranchCode = c.py3_BranchArea,
                    Branch = (c.FormattedValues != null && c.FormattedValues.Contains("py3_brancharea") ? c.FormattedValues["py3_brancharea"] : "N/a"),
                    JobTitle = c.JobTitle,
                    Organisation = (c.ParentCustomerId != null ? c.ParentCustomerId.Name : "N/a"),
                    joinedAsCode = c.py3_SOLACEMemberJoinedAs,
                    JoinedAs = (c.FormattedValues != null && c.FormattedValues.Contains("py3_solacememberjoinedas") ? c.FormattedValues["py3_solacememberjoinedas"] : "N/a"),
                    Expertise = (c.py3_SOLACEMemberAreasofExpertise != null && c.py3_SOLACEMemberAreasofExpertise.Trim() != String.Empty ? c.py3_SOLACEMemberAreasofExpertise : "N/a")
                }
            );

However, this throws the following error:

    Invalid 'where' condition. An entity member is invoking an invalid property or method.

Its the same even if I hard code the branchtag criteria rather than going of the DDL value.
Ive also tried doing a select on the contacts1 set eg:

    var results = contacts1.select(c=> c.BranchTag == ddlBranchTags.SelectedItem.Value

But that throws the same error.

If I remove the branchTag where clause it works as expected.

Likewise if I try performing a search based on any item that contains an empty or null value it returns an error eg:

var contacts =(from c in context.ContactSet
                    join m in context.py3_membershipSet on c.ContactId equals m.py3_Member.Idwhere m.statuscode.Value==1&&((c.FirstName!=null&& c.FirstName== searchTerm)||(c.LastName!=null&& c.LastName== searchTerm)||(c.FullName!=null&& c.FullName== searchTerm))orderby c.LastNameselectnew{ContactId= c.ContactId,FirstName= c.FirstName,LastName= c.LastName,BranchCode= c.py3_BranchArea,Branch=(c.FormattedValues!=null&& c.FormattedValues.Contains("py3_brancharea")? c.FormattedValues["py3_brancharea"]:"N/a"),JobTitle= c.JobTitle,Organisation=(c.ParentCustomerId!=null? c.ParentCustomerId.Name:"N/a"),
                        joinedAsCode = c.py3_SOLACEMemberJoinedAs,JoinedAs=(c.FormattedValues!=null&& c.FormattedValues.Contains("py3_solacememberjoinedas")? c.FormattedValues["py3_solacememberjoinedas"]:"N/a"),Expertise=(c.py3_SOLACEMemberAreasofExpertise !=null&& c.py3_SOLACEMemberAreasofExpertise.Trim()!=String.Empty? c.py3_SOLACEMemberAreasofExpertise :"N/a"),Title= c.Salutation});

Which screams back:

'py3_membership' entity doesn't contain attribute with Name = 'firstname'.

I think its fair to assume that Ive gone wayward with this, so any useful / constructive pointers (for a LINQ newb) would be really appreciated. Thanks.


Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>