cannot convert from System.Collections.Generic.List<> to System.Collections.Generic.IEnumerable<> - c#

Error occurred when trying to add qresult. Anyone can please advise / guide how to fix / amend my code? thanks
cannot convert from System.Collections.Generic.List<> to System.Collections.Generic.IEnumerable<>
public class SystemAccessList
{
public SystemAccess SystemAccess { get; set; }
public List<SystemAccess> SystemAccessList { get; set; }
SystemDbContext db;
public void setDbContext(PALMSConfigDbContext _db)
{
db = _db;
}
public SystemAccessList GetAccessList(SystemAccessList systemAccessList)
{
var qresult = db.tbl_SystemAccessList
.GroupBy(g => g.ClassID)
.AsEnumerable().Select(c =>
{
var rr = new ResultRow { Class = c.Key };
foreach (var r in db.tbl_SystemAccessList.GroupBy(gg => gg.StudentID))
{
rr.Student[r.Key] = "N";
}
foreach (var r in c.Where(w => w.ClassID == c.Key))
{
rr.Student[r.StudentID] = "Y";
}
return rr;
}).ToList();
systemAccessList.SystemAccessList.AddRange(qresult);
return systemAccessList;
}
class ResultRow
{
public ResultRow()
{
Student = new Dictionary<string, string>();
}
public string Class { get; set; }
public IDictionary<string, string> Student { get; set; }
public string GetHeader()
{
return Student.Aggregate("Class", (current, s) => current + "|" + s.Key);
}
public string GetSolidRow()
{
return Student.Aggregate(Class, (current, s) => current + "|" + s.Value);
}
}
public class SystemAccess
{
[Key]
public int ID { get; set; }
public string ClassID { get; set; }
public string StudentID { get; set; }
}

Your qresult will be of type List<ResultRow> because of the Select you have there. A List<ResultRow> is also an IEnumerable<ResultRow> which (when I assume ResultRow is a reference type (class)) by turn is an IEnumerable<Xxx> for any Xxx that is either a base class of ResultRow, or an interface that ResultRow implements.
Your SystemAccessList is of type List<SystemAccess> whose AddRange method than requires an IEnumerable<SystemAccess>.
So all would be fine if a ResultRow were a SystemAccess (by inheritance). But it is not. So you should get a message at compile-time saying the List<ResultRow> is not an IEnumerable<SystemAccess>.

Related

Adding custom logics to Mapster's mapping, possibly with AfterMapping?

After mapping Input to Output, the returned Input must have an incrementing number in its Entries[i].Id, (i.e. Entries[0].Id = 1, Entries[1].Id = 2, Entries[2].Id = 3, ...). So I tried to put it in AfterMapping:
[Mapper]
public interface IMyMapper {
Output Map(Input userTableTemplate);
}
public class RegisterMyMapper : IRegister {
public void Register(TypeAdapterConfig config) {
config.NewConfig<Input, Output>()
.Map(output => output.OutputName, input => input.Name)
.AfterMapping(output => {
foreach (var pair in output.Entries.Select((value, index) => new {value = value, index = index})) {
pair.value.Id = pair.index + 1;
}
});
}
}
public class Output {
public string OutputName { get; set; }
public IEnumerable<Entry> Entries { get; set; }
}
public class Entry { public int Id { get; set; } }
public class Input { public string Name { get; set; } }
But when running _myMapper.Map(myInput) I'm getting a null reference exception, because the private Action<Output> Action1 member of the generated code (public partial class MyMapper : IIMyMapper) is null:
public partial class MyMapper : IMyMapper
{
private Action<Output> Action1;
public Output Map(Input p1)
{
if (p1 == null)
{
return null;
}
Output result = new Output();
result.OutputName = p1.Name;
Action1.Invoke(result);
return result;
}
}

Unable to determine the serialization information when create index

I'm having problem when i create a index.
This is my code:
private static IMongoCollection<IPageItem> GetCollection()
{
return Connection.Database.GetCollection<IPageItem>("SystemPages");
}
internal static IEnumerable<Task> CreateIndex()
{
BsonClassMap.RegisterClassMap<NewsItem>();
var work = new List<Task>();
var coll = GetCollection();
var builder= Builders<IPageItem>.IndexKeys;
var btIndexes = new List<CreateIndexModel<IPageItem>>
{
new(builder.Ascending(x => x.PageId), new CreateIndexOptions { Unique = true })
};
work.Add(coll.Indexes.CreateManyAsync(btIndexes));
return work;
}
public interface IPageItem
{
public Guid PageId { get; set; }
}
public class NewsItem : IPageItem
{
public Guid PageId { get; set; }
public string Summery { get; set; }
}
When i call, CreateIndex() i receive, Unable to determine the serialization information for x => Convert(x.PageId, Object).)
Can I not use one interface when create/get a collection?

System.ArgumentNullException: Value cannot be null Parameter name: value - How to fix this error?

Please help! I am getting error in the line :
details.NominalVoltage = String.Join(",", paneldetails?.NominalVoltage?.ToArray());
I have below code in my builder.
foreach (var panel in panelAddresses.Take(2))
{
var paneldetails = new SM_NFPA72ReportPage1();
details.batteryDetails = new List<Battery>();
var AssociatedPrimaryPowers = new Repository(new BuildConnection()).GetPanelPrimarypowerDevcies(reportInput.UseroId, panel, reportInput.BuildingId, reportInput.TestSessionId[0]).Result;
AssociatedPrimaryPowers.ForEach(x => paneldetails?.batteryDetails?.Add(new Battery
{
NominalVoltage = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "nominalVoltage")?.FirstOrDefault()?.Value,
NominalAmps = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "nominalAmps")?.FirstOrDefault()?.Value,
NominalLocation = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "disconnectLocation")?.FirstOrDefault()?.Value,
Protection = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "overCurrentType")?.FirstOrDefault()?.Value,
ProtectionAmps = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "overCurrentAmps")?.FirstOrDefault()?.Value,
ProtectionLocation = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "powerLocation")?.FirstOrDefault()?.Value,
}));
details.NominalVoltage = String.Join(",", paneldetails?.NominalVoltage?.ToArray());
details.NominalAmps = String.Join(",", paneldetails?.NominalAmps?.ToArray());
details.NominalLocation = String.Join(",", paneldetails?.NominalLocation?.ToArray());
details.Protection = String.Join(",", paneldetails?.Protection?.ToArray());
details.ProtectionAmps = String.Join(",", paneldetails?.ProtectionAmps?.ToArray());
details.ProtectionLocation = String.Join(",", paneldetails?.ProtectionLocation?.ToArray());
}
Below attached is my model for above builder:
public class SM_NFPA72ReportPage1 : IReportModel
{
public string NominalVoltage { get; set; }
public string NominalAmps { get; set; }
public string NominalLocation { get; set; }
public string Protection { get; set; }
public string ProtectionAmps { get; set; }
public string ProtectionLocation { get; set; }
public List<Battery> batteryDetails { get; set; }
public List<PanelDetailsInfo> panelInfo { get; set; }
}
I am reusing the Battery model to fetch the values from repository
public class Battery
{
public string NominalVoltage { get; set; }
public string NominalAmps { get; set; }
public string NominalLocation { get; set; }
public string Protection { get; set; }
public string ProtectionAmps { get; set; }
public string ProtectionLocation { get; set; }
}
The exception tells you that the parameter value is null, that should mean that:
paneldetails?.NominalVoltage?.ToArray()
...gives you a null result, and that the string.Join method does not accept it.
You need to make sure that you do not provide a null value to the method.
This can be achieved in multiple ways, for example by checking for null value before calling the method:
if (panelDetails?.NominalVoltage != null)
{
details.NominalVoltage = String.Join(",", paneldetails.NominalVoltage.ToArray());
}
or by returning a empty array by default if it is null:
details.NominalVoltage = String.Join(",", paneldetails?.NominalVoltage?.ToArray() ?? Array.Empty<string>());

C# Generic search in object recursively [duplicate]

This question already has answers here:
Recursively Get Properties & Child Properties Of A Class
(5 answers)
Closed 2 years ago.
I am trying to write an universal search to use for all objects.
I have this code, which is working fine to search in just one object's properties, but I would also like to search also in properties in related objects.
Eg. I have these Models/Objects
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Address{ get; set; }
public ICollection<Contract> Contracts { get; set; }
}
public class Contract
{
public int Id { get; set; }
public DateTime From{ get; set; }
public DateTime To{ get; set; }
public string Comment{ get; set; }
public int CustomerId { get; set; }
[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}
and I want to search if any of properties contains some a string eg. "Peter", I will call it this way:
string searchString = "Peter";
var customers = db.Customers
.Include(x => x.Contracts)
.WhereAnyPropertiesOfSimilarTypeContains(searchString);
this code will check if any properties of 'Customer' contains string "Peter".
But I would also need to check if the related model 'Contract' contains "Peter.
public static class EntityHelper
{
public static IQueryable<TEntity> WhereAnyPropertiesOfSimilarTypeContains<TEntity, TProperty>(this IQueryable<TEntity> query, TProperty value)
{
var param = Expression.Parameter(typeof(TEntity));
var predicate = PredicateBuilder.False<TEntity>(); //--- True to equal
var entityFields = GetEntityFieldsToCompareTo<TEntity, TProperty>();
foreach (var fieldName in entityFields)
{
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var predicateToAdd = Expression.Lambda<Func<TEntity, bool>>(
Expression.Call(
Expression.PropertyOrField(param, fieldName), method,
Expression.Constant(value)), param);
predicate = predicate.Or(predicateToAdd); //--- And to equal
}
return query.Where(predicate);
}
// TODO: You'll need to find out what fields are actually ones you would want to compare on.
// This might involve stripping out properties marked with [NotMapped] attributes, for
// for example.
public static IEnumerable<string> GetEntityFieldsToCompareTo<TEntity, TProperty>()
{
Type entityType = typeof(TEntity);
Type propertyType = typeof(TProperty);
var fields = entityType.GetFields()
.Where(f => f.FieldType == propertyType)
.Select(f => f.Name);
var properties = entityType.GetProperties()
.Where(p => p.PropertyType == propertyType)
.Select(p => p.Name);
return fields.Concat(properties);
}
}
Thanks.
After reread the question. I don't know what are you trying, but here I put the idea I have what are you looking for.
public class Customer : AbstractEntity
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public ICollection<Contract> Contracts { get; set; }
}
public class Contract : AbstractEntity
{
//what property here can be string "Peter"? Comments?
//what are you trying?
public int Id { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
public string Comment { get; set; }
public int CustomerId { get; set; }
[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}
public abstract class AbstractEntity
{
//this method can be used to preselect properties you want
protected virtual Tuple<bool, ICollection<PropertyInfo>> PropertyCollector()
{
return new Tuple<bool, ICollection<PropertyInfo>>(false, null);
}
public IEnumerable<Tuple<Type, object>> GetRowValues()
{
foreach (var prop in GetRows())
{
yield return new Tuple<Type, object>(prop.PropertyType, prop.GetValue(this));
}
}
public ICollection<PropertyInfo> GetRows()
{
var tuple = PropertyCollector();
ISet<PropertyInfo> pInfo;
if (tuple.Item1)
{
pInfo = new HashSet<PropertyInfo>(tuple.Item2);
}
else //search all non virtual, private, protected properties, "following POCO scheme"
{
pInfo = new HashSet<PropertyInfo>();
foreach (var prop in GetType().GetProperties())
{
foreach (var access in prop.GetAccessors())
{
if ((!access.IsVirtual && !access.IsPrivate) && (prop.CanWrite && prop.CanRead))
{
pInfo.Add(prop);
}
}
}
}
return pInfo;
}
}
public static class Searchs
{
public static ICollection<object> ObjectsWithStringFound(ICollection<Customer> customers, string toBeFound)
{
var objs = new List<object>();
foreach (var cust in customers)
{
var strings = cust.GetRowValues().Where(tpl => tpl.Item1 == typeof(string)).Select(tpl => tpl.Item2);
var contracts = cust.GetRowValues().Where(tpl => tpl.Item2 is IEnumerable<Contract>).Select(tpl => tpl.Item2);
if (strings.Any(str => str == toBeFound))
{
objs.Add(cust);
}
else if (contracts.Any(ctr => ((IEnumerable<Contract>)ctr).!!!!!!!!! == toBeFound))
{ //What I suppose I must "match" with "Peter"??!?!
objs.Add(contracts.First(ctr => ((IEnumerable<Contract>)ctr).!!!!!!!!! == toBeFound));
}
}
return objs;
}
}
I think we aren't understanding each other.

Dynamic class based on string parameter

I have this:
public class Blah
{
public int id { get; set; }
public string blahh { get; set; }
}
public class Doh
{
public int id { get; set; }
public string dohh { get; set; }
public string mahh { get; set; }
}
public List<???prpClass???> Whatever(string prpClass)
where string prpClass can be "Blah" or "Doh".
I would like the List type to be class Blah or Doh based on what the string prpClass holds.
How can I achieve this?
EDIT:
public List<prpClass??> Whatever(string prpClass)
{
using (var ctx = new ApplicationDbContext())
{
if (prpClass == "Blah")
{
string queryBlah = #"SELECT ... ";
var result = ctx.Database.SqlQuery<Blah>(queryBlah).ToList();
return result;
}
if (prpClass == "Doh")
{
string queryDoh = #"SELECT ... ";
var result = ctx.Database.SqlQuery<Doh>(queryDoh).ToList();
return result;
}
return null
}
}
you have to have a common supertype:
public interface IHaveAnId
{
int id { get;set; }
}
public class Blah : IHaveAnId
{
public int id { get; set; }
public string blahh { get; set; }
}
public class Doh : IHaveAnId
{
public int id {get;set;}
public string dohh { get; set; }
public string mahh { get; set; }
}
then you can do:
public List<IHaveAnId> TheList = new List<IHaveAnId>();
and in some method:
TheList.Add(new Blah{id=1,blahh = "someValue"});
TheList.Add(new Doh{id =2, dohh = "someValue", mahh = "someotherValue"});
to iterate through the list:
foreach(IHaveAnId item in TheList)
{
Console.WriteLine("TheList contains an item with id {0}", item.id);
//item.id is allowed since you access the property of the class over the interface
}
or to iterate through all Blahs:
foreach(Blah item in TheList.OfType<Blah>())
{
Console.WriteLine("TheList contains a Blah with id {0} and blahh ='{1}'", item.id, item.blahh);
}
Edit:
the 2 methods and a int field holding the autovalue:
private int autoValue = 0;
public void AddBlah(string blahh)
{
TheList.Add(new Blah{id = autovalue++, blahh = blahh});
}
public void AddDoh(string dohh, string mahh)
{
TheList.Add(new Doh{id = autovalue++, dohh = dohh, mahh = mahh});
}
Another Edit
public List<object> Whatever(string prpClass)
{
using (var ctx = new ApplicationDbContext())
{
if (prpClass == "Blah")
{
string queryBlah = #"SELECT ... ";
var result = ctx.Database.SqlQuery<Blah>(queryBlah).ToList();
return result.Cast<object>().ToList();
}
if (prpClass == "Doh")
{
string queryDoh = #"SELECT ... ";
var result = ctx.Database.SqlQuery<Doh>(queryDoh).ToList();
return result.Cast<object>.ToList();
}
return null;
}
}
in the view you then have to decide what type it is. In asp.net MVC you can use a display template and use reflection to get a good design. But then i still don't know what technology you are using.
Yet another Edit
TestClass:
public class SomeClass
{
public string Property { get; set; }
}
Repository:
public static class Repository
{
public static List<object> Whatever(string prpClass)
{
switch (prpClass)
{
case "SomeClass":
return new List<SomeClass>()
{
new SomeClass{Property = "somestring"},
new SomeClass{Property = "someOtherString"}
}.Cast<object>().ToList();
default:
return null;
}
}
}
And a controller action in mvc:
public JsonResult Test(string className)
{
return Json(Repository.Whatever("SomeClass"),JsonRequestBehavior.AllowGet);
}
then i called it with: http://localhost:56619/Home/Test?className=SomeClass
And got the result:
[{"Property":"somestring"},{"Property":"someOtherString"}]
Is this what you are trying to do?
public class Blah
{
public int id { get; set; }
public string blahh { get; set; }
}
public class Doh
{
public int id { get; set; }
public string dohh { get; set; }
public string mahh { get; set; }
}
class Program
{
public static List<T> Whatever<T>(int count) where T: new()
{
return Enumerable.Range(0, count).Select((i) => new T()).ToList();
}
static void Main(string[] args)
{
var list=Whatever<Doh>(100);
// list containts 100 of "Doh"
}
}

Categories

Resources