I have a class that acts as a summary which is going to be passed to an MVC View as an IEnumerable. The class looks like:
public class FixedLineSummary
{
public long? CustomerId { get; set; }
public string CustomerName { get; set; }
public int? NumberOfLines { get; set; }
public string SiteName { get; set; }
}
The results returned from the db have all of the single entries and so I use linq to summarise these using:
var summary = (from r in result
let k = new {r.CustomerId, CustomerName = r.CompanyName, r.SiteName}
group r by k into t
select new
{
t.Key.CustomerId,
t.Key.CustomerName,
t.Key.SiteName,
Lines = t.Sum(r => r.lines)
});
When I try and cast the results into my object however I just keep getting an error that:
Instance argument: cannot convert from 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<Domain.Entities.FixedLineSummary>'
Is there a way to cast the results of the linq query into an enumerable of my class?
You should change the projection to create your class, rather than an anonymous type:
var summary = from r in result
let k = new {r.CustomerId, CustomerName = r.CompanyName, r.SiteName}
group r by k into t
select new FixedLineSummary
{
CustomerId = t.Key.CustomerId,
CustomerName = t.Key.CustomerName,
SiteName = t.Key.SiteName,
NumberOfLines = t.Sum(r => r.lines)
};
You cannot cast the anonymous type to FixedLineSummary since both are not related at all(for the compiler). Instead you need to create instances of your class manually:
IEnumerable<FixedLineSummary> summaries = summary
.Select(s => new FixedLineSummary
{
CustomerId = s.CustomerId,
CustomerName = s.CustomerName,
NumberOfLines = s.NumberOfLines,
SiteName = s.SiteName
})
.ToList();
Related
I am currently loading two Orders and Colors tables, I wanted the Colors table to list the items that have the ID equal to Orders. For this, what occurred to me was to assign the IdOrders values to a variable and compare it with my IdOrders (in my table Colors), but it is not possible to assign the database's balance to my variable
My tables:
public partial class Orders
{
public int ID_Orders { get; set; }
public Nullable<System.DateTime> Data_Registo { get; set; }
public string Num_Encomenda { get; set; }
public string Ref_Cliente { get; set; }
}
public partial class Colors
{
public int ID_Orders { get; set; }
public int ID_Programa_Malha { get; set; }
public int ID_Linha_Cor { get; set; }
public string Cor { get; set; }
}
I am working with a database already in operation and possible these tables are already used in a sql join but not how to process that information.
As I said the first thing I remembered was to do this:
My Controller:
var id = from d in db.Orders
select d.ID_Orders;
var color = db.Colors.Where(x => x.ID_Orders = id).ToList();
var tables = new EncomendaViewModel
{
Orders= db.Orders.ToList(),
Colors= color.ToList(),
};
return View(tables);
Error in id: CS0029 C# Cannot implicitly convert type to 'int'
Is it possible to process the data in this way?
Thanks for anyone who can help!
-------------------(Update)------------------------------------------------
Using == cs0019 operator '==' cannot be applied to operands of type
My view in Broswer
dbEntities sd = new dbEntities();
List<Orders> orders= sd.Orders.ToList();
List<Colors> colers= sd.Colors.ToList();
var multipletable = from c in orders
join st in colers on c.ID_Programa equals st.ID_Programa into table1
from st in table1.DefaultIfEmpty()
select new MultipleClass { orders= c, colers= st };
There could be one or more values returned from the below query.
var id = from d in db.Orders
select d.ID_Orders;
That is the reason why it was throwing an error.
So lets try it this way
var color = db.Colors.Where(x => id.Contains(x.ID_Orders)).ToList();
public class OrderWithColorsViewModel
{
public Order order { get; set; }
public List<Colors> colers{ get; set; }
}
Public class TestOrderController : Controller
{
public DailyMVCDemoContext db = new DailyMVCDemoContext();
public ActionResult Index()
{
var orders= db.Orders.ToList();
var colers = db.Colors.ToList();
var result = (from c in orders
join st in colers on c.ID_Orders equals st.id into table1
select new OrderWithColorsViewModel { order =c, colers =
table1.ToList() }).ToList();
return View(result);
}
}
credits: YihuiSun
I have a list of two properties named Kanjis :
and I would like to use that to update the contents of another list named PhraseSources
public class PhraseSource
{
[PrimaryKey]
public string Id { get; set; }
public int PhraseNum { get; set; }
public string English { get; set; }
public string Kanji { get; set; }
public string WordType { get; set; }
public string FrequencyA { get; set; }
}
by matching together Kanji > Text and updating FrequencyA with Code
Is this something that can be done with LINQ or is there a better to way to do this by iterating through each of the rows of the phraseSource, checking for a matching entry in Kanjis and doing an update that way?
Here's the code suggested by Salva that I tried:
(from sa in source
join d in psDb on sa.Text equals d.Kanji
let temp = d.FrequencyA = sa.Code
select 0).ToList();
gives error:
ApplyFrequency.cs(14,14): Error CS1941: The type of one of the
expressions in the join clause is incorrect. Type inference failed in
the call to 'Join'. (CS1941) (Download)
source.Join(psDb, s => s.Text, d => d.Kanji, (s, d) => d.FrequencyA = s.Code).ToList();
gives error:
ApplyFrequency.cs(21,21): Error CS0411: The type arguments for method
'Enumerable.Join(IEnumerable,
IEnumerable, Func, Func,
Func)' cannot be inferred from the usage. Try
specifying the type arguments explicitly. (CS0411) (Download)
Notes:
I had to use this code as I was asked to change FrequencyA to an int:
var source = original
.Select(x => new
{
Text = x.Text,
Code = Convert.ToInt32(x.Code.Substring(2))
})
.ToList();
You can do it via single linq query syntax:
var source = new List<TextCodeClass>();
var dest = new List<PhraseSource>();
(from s in source
join d in dest on s.Text equals d.Kanji
let temp = d.FrequencyA = s.Code.ToString()
select 0).ToList();
or via method syntax:
source.Join(dest, s => s.Text, d => d.Kanji, (s, d) => d.FrequencyA = s.Code.ToString())
.ToList();
You can update list through LINQ only. The closest you could get is:
static void Main(string[] args)
{
List<SomeClass> scl = new List<SomeClass>();
List<OtherClass> ocl = new List<OtherClass>();
foreach (var item in scl)
item.FrequencyA = ocl.Where(i => i.Text == item.Kanji).FirstOrDefault()?.Code ?? null;
}
// sample classes that reflect relevant properties
public class SomeClass
{
public string FrequencyA { get; set; }
public string Kanji { get; set; }
}
public class OtherClass
{
public string Code { get; set; }
public string Text { get; set; }
}
So i want to achieve something like:
var query = from p in db.Project
select new A
{
Project = p,
Capacity = new List<Capacity>((from pp in db.ProjectActualCapacity
where pp.ProjectID == p.ID
select new Capacity
{
Actual = pp.Hours,
Date = pp.Date,
ProjectID = pp.ProjectID
}
).ToList())
};
However, when the query is converted to list. It throws the following error
Only parameterless constructors and initializers are supported in LINQ to Entities.
Is there a workaround to this?
thanks
//Update
public class Capacity
{
public DateTime Date { get; set; }
public decimal? Actual { get; set; }
public decimal? Projected { get; set; }
public int ProjectID { get; set; }
public decimal Rate { get; set; }
}
You are explicitly creating a list and using the constructor which accepts an enumerable. This is not necessary since you are already using .ToList() where you define that collection.
Also, your Capacity class needs a parameterless constructor.
So I think it will work like this.
var query = from p in db.Project
select new A {
Project = p,
Capacity = (from pp in db.ProjectActualCapacity
where pp.ProjectID == p.ID
select new Capacity {
Actual = pp.Hours,
Date = pp.Date,
ProjectID = pp.ProjectID
}
).ToList())
};
New at linq to entities trying to figure this out. I have the following tables:
Customer: Cust_Id, Name
Orders: Order_Id
CustomerOrders: Cust_Id, Order_Id
I have a class like so:
public class Customers
{
public List<Row> Rows { get; set; }
public Customers()
{
Rows = new List<Row>();
}
public class Row
{
public int Key { get; set; }
public string Name { get; set; }
public List<string> Order_Ids { get; set; }
}
}
Linq query is like this:
var query = from c in context.Customer
select new Customers.Row
{
Key = c.Cust_Id,
Name = c.Name,
Order_IDs = List<string>( ?? )
};
foreach (var row in query)
{
Customers.Rows.Add(row);
}
var serializer = new JavaScriptSerializer();
return serializer.Serialize(Customers);
Where I have '??', can I use a subquery or something to get a list of Order_Id's from the CustomerOrders table?
Right Now, I can only think to loop through the Customers table once it is filled and then query the DB again to get each array of Order Id's for each Customer.
If it's not a requirement, drop the "Row" collection from the "Customer" object. This should suffice:
public class Customer
{
public int Key { get; set; }
public string Name { get; set; }
public List<string> Order_Ids { get; set; }
}
Then you can query like this:
var customers = from c in context.Customers
select new Customer
{
Key = c.Cust_Id,
Name = c.Name,
Order_IDs = c.Orders.Select(o => o.Order_Id).ToList()
};
It's better to deal in objects when writing C# and using EF, than to deal in terms of tables and rows -less confusing.
Try something like this:
var query = from c in context.Customer
select new Customers.Row
{
Key = c.Cust_Id,
Name = c.Name,
Order_Ids = c.Rows.Select(row => row.Key.ToString()).ToList()
};
Where you have .Select(row => row.Key.ToString()) you can set the property you need (Key, Name, etc...). Select method is an extension method to IEnumerable and it return a collection of type of property you have seted, in this case, a collection of strings because I converted it with ToString() method.
I have the following class
public class Booking
{
public string Group { get; set; }
public BookingType Type { get; set; }
public BookingStatus Status { get; set; }
public InvoiceFreq { get; set; }
public InvoiceLevel { get; set; }
}
That I'd need to group them by all properties in Linq so that the last two properties form one new field in the grouped result.
Like this:
Group Type Status InvoiceType
-------------------------------------------
Group1 Online New Daily-Single
Group2 Phone Accepted Daily-Single
Group3 Store Accepted Weekly-Single
Group3 Store Accepted Weekly-Bulk
Here the InvoiceFreq will be Daily / Weekly and the InvoiceLevel = Single or Bulk.
The query I have so far is this:
var query = from b in Bookings
group b by new
{
b.Group,
b.Type,
b.Status,
b.InvoicingFrequency,
b.InvoicingLevel
} into bookingGroup
select new BookingSummaryLine()
{
Group = bookingGroup.Key.UserGroup,
Type = bookingGroup.Key.UserGroup,
Status = bookingGroup.Key.FinancialStatus,
InvType = String.Format({0}-{1},bookingGroup.Key.InvoicingFrequency, bookingGroup.Key.InvoicingLevel
};
This of course does not give the desired result as the two properties are in the anonymous type separately.
Is there any way to achieve this in Linq?
This should work:
var query = from b in Bookings
group b by new
{
b.Group,
b.Type,
b.Status,
InvType = String.Format({0}-{1},b.InvoicingFrequency, b.InvoicingLevel)
} into bookingGroup
select new BookingSummaryLine()
{
Group = bookingGroup.Key.UserGroup,
Type = bookingGroup.Key.UserGroup,
Status = bookingGroup.Key.FinancialStatus,
InvType = bookingGroup.Key.InvType
};
I'm not as sure about the syntax for linq but for lambda I would use this. Basically use distinct if your trying to group on all columns like that.
Context.Booking.Distinct().Select(z=> new BookingSummaryLine { Group = z.Group, Type = z.Type, Status = z.Status, InvType = (z.InvoiceFrequency + "-" + z.InvoiceLevel });