Remove duplicate objects in list in C#

Just sharing a sample code to remove duplicates from a list using linq


public class MyClass
{
public string ID { get; set; }
public string Value { get; set; }

}

 

List<MyClass> myList = new List<MyClass>();
var xrmOptionSet = new MyClass();
xrmOptionSet.ID = "1";
xrmOptionSet.Value = "100";
var xrmOptionSet1 = new MyClass();
xrmOptionSet1.ID = "2";
xrmOptionSet1.Value = "200";
var xrmOptionSet2 = new MyClass();
xrmOptionSet2.ID = "1";
xrmOptionSet2.Value = "100";
myList.Add(xrmOptionSet);
myList.Add(xrmOptionSet1);
myList.Add(xrmOptionSet2);

// here we are first grouping the result by label and then picking the first item from each group
var myDistinctList = myList.GroupBy(i => i.ID)
.Select(g => g.First()).ToList();

&nbsp;

Hope it helps..

 

Fix: Invalid ‘where’ condition. An entity member is invoking an invalid property or method while using LINQ in CRM 2013.

Hi,

We were writing a LINQ query to get the contract record information by passing in the lawyer name. The lawyer is a lookup in the contract record.

So we were using contract.lawyerid.Name field of lookup in our where condition.

However we got the below error while doing so..

 

It seems like the LINQ Implementation for CRM doesn’t correctly interpret the lookup field’s name if we are using it in the where clause.

The solution was to do a join between the entities and then use the where condition against the lawyer entity username field itself.

Hope it helps..

Wonderful tools to learn Linq and oData Query for CRM 2011

Check out these wonderful tools to lear Linq and oData Query

Bye.

LINQ to Microsoft Dynamics CRM

Suppose this is our entity’s schema name “new_test

and it contains following fields

new_name

new_lastname

Using CrmSvcUtil  we have created the Entities classes and Data Context class.

https://nishantrana.wordpress.com/2010/08/11/using-crmsvcutil/

Now this is how we could use LINQ to query data using query expression as well as method based query.

Create the instace of DataContext class named xrm

var myXrm = new xrm(“CRMOnPremise”);

To loop through all the test records


foreach (var testRecord in myXrm.new_tests)
{
// print the information
}

To select a specific record


var singleRecord = (from myTest in myXrm.new_tests
where myTest.new_lastname == “Rana”
select myTest).Single();

var singleRecord1 = myXrm.new_tests
.Single(t => t.new_lastname == “Rana”);

To select all the records having last name as Rana

var allRecord = from myTest in myXrm.new_tests
where myTest.new_lastname == “Rana”
select myTest;

var allRecords1 = myXrm.new_tests.
Where(t => t.new_lastname == “Rana”);

To order the records

var allRecOrder=from myTest in myXrm.new_tests
orderby myTest.new_name  ascending
where myTest.new_lastname==“Rana”
select myTest;

var allRecOrder1 = myXrm.new_tests
.OrderBy(t => t.new_name)
.Where(t => t.new_lastname == “Rana”);

To select  specific field instead of the entire record


var singleField = from myTest in myXrm.new_tests
select myTest.new_name;

var singleField1 = myXrm.new_tests
.Select(t => t.new_name);

To return specific fields

var specificFields = from myTest in myXrm.new_tests
select new { myTest.createdby, myTest.createdon };

var specificFields1 = myXrm.new_tests
.Select(t => new { t.createdby, t.createdon });

Use of Take and Skip function
Take returns the given number of elements and ignores the rest
Skip skips the given number of elements and yielding the rest

var takeField = (from myTest in myXrm.new_tests
select myTest.new_name).Take(2);

var takeField1 = myXrm.new_tests
.Take(2)
.Select(t => t.new_name);

var skipField = (from myTest in myXrm.new_tests
select myTest.new_name).Skip(2);

var skipField1 = myXrm.new_tests
.Skip(2)
.Select(t => t.new_name);

Join – similar to inner join
The ‘select’ and ‘orderBy’ calls may only reference

a single common entity type.
We will get above error if we try to retrieve value from  the other entity involved in join
var joinRecords = from t in myXrm.new_tests
join s in myXrm.systemusers on
t.ownerid.Value equals s.systemuserid
select new {t.new_name  };

var joinRecords1=myXrm.new_tests
.Join(myXrm.systemusers,
t=>t.ownerid.Value,
s=>s.systemuserid ,
(t,s)=>new {t.new_name});

Where conditions with Contains,StartsWith, EndsWith and

Equal string functions

var test1 = from p in myXrm.new_tests
where p.new_name.Contains(“R”)
select p;
var test2 = from p in myXrm.new_tests
where p.new_name.StartsWith(“R”)
select p;
var test3= from p in myXrm.new_tests
where p.new_name.EndsWith(“D”)
select p;

var test11 = myXrm.new_tests
.Where(t => t.new_name.Contains(“R”));
var test22 = myXrm.new_tests
.Where(t => t.new_name.StartsWith(“R”));
var test32 = myXrm.new_tests
.Where(t => t.new_name.EndsWith(“D”));

Download the project :-

http://www.box.net/shared/xbg0xd5p7m

Bye..