Search code examples
linqexpression-treesdynamic-linqlinq-expressionsdynamic-queries

Dynamic Linq Library can’t handling duplicate alias column names


I am trying to collect the data from database by using Dynamic Linq Library NeGet. When I loop through it showing this error ‘The item with identity 'FirstName' already exists in the metadata collection’

Seems like Dynamic Linq Library NuGet is can’t handle duplicate alias column names.

I have two tables and it has One-On-Many relationship as follow, containing the same Columns names in Lead table and CoBorrower table.

Table1: Lead

Columns: LeadID, FirstName,DateCreated

Table2: CoBorrower

Columns: LeadID, CoBorrowerID,FirstName,Tax,DateCreated

Here is my code snippet

var res = (from l in db.Leads
join db.CoBorrower.GetAll()
on l.LeadID equals cb.LeadID
select new { l, cb }).AsQueryable();
string myCustomCondition="FistName=myname";
IQueryable iq = res.Where(myCustomCondition)
                .OrderBy(reportBulder.Group1)
                .Select("new(l.LeadID,l.FirstName,cb.FistName)")
                .GroupBy("LeadID", "it")
                .Select("new (it.Key as Key, it as Value)");
foreach (dynamic group in iq)
{
 string Key = group.Key.ToString();
 List<dynamic> items = new List<dynamic>();
  foreach (dynamic album in group.Value)
   {
    items.Add(album);
   }
 dataList.Add(Key, items);
}

enter image description here

I will appreciate your help in advance.


Solution

  • This is logical. The .Select("new(l.LeadID,l.FirstName,cb.FistName)") will create an anonymous object like:

    new 
    {
        LeadID = ....,
        FirstName = ....,
        FirstName = ....,
    }
    

    that is illegal (two properties with the same name)

    Use the as

    .Select("new(l.LeadID,l.FirstName,cb.FistName as FirstName2)")
    

    so that the anonymous object created is

    new 
    {
        LeadID = ....,
        FirstName = ....,
        FirstName2 = ....,
    }
    

    as you do in the second .Select.