Convert from List into IEnumerable format
You don't need to convert it. List<T>
implements the IEnumerable<T>
interface so it is already an enumerable.
This means that it is perfectly fine to have the following:
public IEnumerable<Book> GetBooks()
{
List<Book> books = FetchEmFromSomewhere();
return books;
}
as well as:
public void ProcessBooks(IEnumerable<Book> books)
{
// do something with those books
}
which could be invoked:
List<Book> books = FetchEmFromSomewhere();
ProcessBooks(books);
how to convert list to IEnumerable?
Problem is not in convertation of List<T>
to IEnumerable<T>
. Becuase List<T>
implements IEnumerable<T>
.
Your problem is that generic parameters are different. You are trying to convert List<T1>
to IEnumerable<T2>
. Where:
- T1 is
QlasrService.EntityFramework.tblSoftwareImageTestPlan
- T2 is
QlasrService.Model.SchemaModels.LAP.SoftwareImageTestPlan
Simplest solution will be mapping (either manual or automatic). Automatic mapping is very easy. Add Automapper nuget package. Place this line somewhere on application start:
Mapper.Initialize(cfg => cfg.CreateMap<tblSoftwareImageTestPlan, SoftwareImageTestPlan>());
And now your method will look like:
public IEnumerable<SoftwareImageTestPlan> GetAssignedTestPlansForSPSI(
int SoftwareProductID, int SoftwareImageID)
{
var testPlans = from tp in _entities.tblSoftwareImageTestPlans
where tp.SoftwareProductID == SoftwareProductID && tp.SoftwareImageID == SoftwareImageID
select tp;
return Mapper.Map<IEnumerable<SoftwareImageTestPlan>>(testPlans);
}
NOTE: In your code either records
cannot have null
value, or you will have NullReferenceException
at ToList()
call. So if..else
block is useless anyway.
Freely convert between List<T> and IEnumerable<T>
List<string> myList = new List<string>();
IEnumerable<string> myEnumerable = myList;
List<string> listAgain = myEnumerable.ToList();
converting list<string> to IEnumerable<guid> inside an argument
You could use both the LINQ Select
method and Guid.Parse
to convert the List<string>
to IEnumerable<Guid>
.
The Select
here will:
Projects each element of a sequence into a new form by incorporating the element's index.
and the Guid.Parse
will:
Converts the string representation of a GUID to the equivalent Guid structure.
So your code will become:
Location = ScHelper.Instance().GetContentPageToValueVo(x.Locations.Select(Guid.Parse)),
Service = ScHelper.Instance().GetContentPageToValueVo(x.Services.Select(Guid.Parse))
In order to avoid ArgumentNullException
, you could use that:
Guid y;
Location = ScHelper.Instance().GetContentPageToValueVo(x.Locations.Where(x=>!string.IsNullOrWhiteSpace(x) && Guid.TryParse(x,out y)).Select(Guid.Parse)),
Service = ScHelper.Instance().GetContentPageToValueVo(x.Services.Where(x=>!string.IsNullOrWhiteSpace(x) && Guid.TryParse(x,out y)).Select(Guid.Parse))
Convert List Type to IEnumerable Interface Type
If you're in .NET 4.0 or later, you can just do an implicit cast:
IEnumerable<IPerson> iPersonList = personlist;
//or explicit:
var iPersonList = (IEnumerable<IPerson>)personlist;
This uses generic contravariance in IEnumerable<out T>
- i.e. since you only ever get something out of an IEnumerable
, you can implicitly convert IEnumerable<T>
to IEnumerable<U>
if T : U
. (It also uses that List<T> : IEnumerable<T>
.)
Otherwise, you have to cast each item using LINQ:
var iPersonList = personlist.Cast<IPerson>();
How to convert List<object> to IEnumerable<T>
Assuming that expected
really contains instances of type T
, you can use the LINQ Cast operator:
public IEnumerable<T> SqlQuery<T>(string sql)
{
return expected.Cast<T>();
}
InvalidCastException will be thrown if the cast fails for any element.
Group List and Convert to IEnumerable
You probably want the total quantity by product Id:
var groupedProductList = products
.GroupBy(u => u.ProductId)
.Select(g => (ProductId: g.Key, Quantity: g.Sum(p => p.Quantity)));
This is achieved by creating a tuple in the Select-clause. The product Id is the key of the group (since we grouped by this Id). Instead of retrieving a enumeration of products in the group, we sum the quantities of these products.
Note that your original query yields a IEnumerable<IEnumerable<(int, int)>>
, i.e., nested enumerations.
This solution returns a simple enumeration: IEnumerable<(int ProductId, int Quantity)>
compatible with your builder method.
Related Topics
C# Linq Using Null or Empty Strings in a Where Statment
How to Return a File (Filecontentresult) in ASP.NET Webapi
C# How to Split a List in Two Using Linq
Convert String to Given Regex Pattern
How to Get Column Names of Table At Runtime in C#
How to Open the Default Chrome Profile Through Selenium, Chromedriver and Googlechrome
Find Item in Observablecollection Without Using a Loop
Asp.Net Core 2 Web API Timeout Issue
Response to Preflight Request Doesn't Pass Access Control Check (Angular2)
Change Datagrid Cell Colour Based on Values
What Could Cause an Assignment to Not Work
How to Loop Through Each and Every Row, Put Value in Gridview Column Cells
Could Not Start Windows Service, Error 1064
Why Am I Getting Error Cs0246: the Type or Namespace Name Could Not Be Found
C# How to Simply Encrypt a Text File With a Pgp Public Key