I have a class and 2 subclasses:
public class User
{
public string eRaiderUsername { get; set; }
public int AllowedSpaces { get; set; }
public ContactInformation ContactInformation { get; set; }
public Ethnicity Ethnicity { get; set; }
public Classification Classification { get; set; }
public Living Living { get; set; }
}
public class Student : User
{
public Student()
{
AllowedSpaces = AppSettings.AllowedStudentSpaces;
}
}
public class OrganizationRepresentative : User
{
public Organization Organization { get; set; }
public OrganizationRepresentative()
{
AllowedSpaces = AppSettings.AllowedOrganizationSpaces;
}
}
I have created a data model to capture form data and to return the correct object type for the user:
public class UserData
{
public string eRaiderUsername { get; set; }
public int Ethnicity { get; set; }
public int Classification { get; set; }
public int Living { get; set; }
public string ContactFirstName { get; set; }
public string ContactLastname { get; set; }
public string ContactEmailAddress { get; set; }
public string ContactCellPhone { get; set; }
public bool IsRepresentingOrganization { get; set; }
public string OrganizationName { get; set; }
public User GetUser()
{
var user = (IsRepresentingOrganization) ? new OrganizationRepresentative() : new Student();
}
}
However, my ternary operation in the GetUser() method is failing with this error:
Type of conditional expression cannot be determined because there is no implicit conversion between {namespace}.OrganizationRepresentative and {namespace}.Student.
What am I missing?
You have to explicitly cast the first branch of the ternary expression to the base type (User) so that the compiler can determine what type the expression can evaluate to.
var user = (IsRepresentingOrganization)
? (User)new OrganizationRepresentative()
: new Student();
The compiler won't automatically deduce which base type should be used for the expression, so you have to specify it manually.
Related
Anyone have any idea / example how can I get the list of DynamoDbRepo with all nested structure if in DynamoDbMethodParameter.Name == specific value?
I suppose I should use ScanAsync but with what setting( ScanRequest ) ?
[DynamoDBTable("REPO_TABLE")]
public class DynamoDbRepo
{
[DynamoDBProperty("Id")]
[DynamoDBHashKey]
public int Id { get; set; }
[DynamoDBProperty("Solutions")]
public List<DynamoDbSolution> Solutions { get; set; }
}
public class DynamoDbSolution
{
[DynamoDBProperty("Path")]
public string Path { get; set; }
[DynamoDBProperty("Methods")]
public List<DynamoDbMethod> Methods { get; set; }
}
public class DynamoDbMethod
{
[DynamoDBProperty("Name")]
public string Name { get; set; }
[DynamoDBProperty("MethodParameters")]
public List<DynamoDbMethodParameter> MethodParameters { get; set; }
}
public class DynamoDbMethodParameter
{
[DynamoDBProperty("Type")]
public string Type { get; set; }
[DynamoDBProperty("Name")]
public string Name { get; set; }
}
I have a class Loan:
public class Loan
{
public int ID { get; set; }
public int ClientID { get; set; }
public string PropertyAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
//etc..
}
And a class Client:
public class Client
{
public int ID { get; set; }
public string Company { get; set; }
public string FirstName { get; set; }
public string Address { get; set; }
// etc..
}
I need a ClientWithLoan object, as there is no multiple inheritance in C# what would be the correct pattern for that?
Two options:
Distinct class
If a ClientWithLoan is to be a distinct type from a Client then you could do it this way:
class ClientWithLoan : Client
{
public Loan Loan { get; set; }
}
You might also want to include some validation:
class ClientWithLoan : Client
{
protected Loan _loan;
public Loan Loan
{
get { return _loan; }
set
{
if (value.ClientID != this.ID) throw ArgumentException();
_loan = value;
}
}
}
Keep what you have
Just add a Loan property to your Client class, and leave it null if that particular client has no loan.
public class Client
{
public int ID { get; set; }
public string Company { get; set; }
public string FirstName { get; set; }
public string Address { get; set; }
public Loan Loan { get; set; }
}
When you require multiple inheritance in a language with single inheritance, using interfaces usually solves the issue.
You could do this in this way:
public interface ILoan
{
public int ID { get; set; }
public int ClientID { get; set; }
public string PropertyAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
//etc..
}
public interface IClient
{
public int ID { get; set; }
public string Company { get; set; }
public string FirstName { get; set; }
public string Address { get; set; }
// etc..
}
public interface IClientWithLoan: IClient, ILoan
{
}
public sealed class ClientWithLoan: IClientWithLoan
{
// here place the real implementation
}
This approach gives you the flexibility you ask.
I have a ViewModel
public class SubjectOverviewViewModel
{
[DisplayName("Neptunkód")]
public Subject NeptunId { get; set; }
public SubjectContent TaFoAdatok { get; set; }
public SubjectContent TaAdatok { get; set; }
public SubjectContent TaOktatok { get; set; }
public SubjectContent TaKurzusok { get; set; }
public SubjectContent IrodalomLista { get; set; }
public virtual SubjectContent SubjectContent { get; set; }
public virtual Subject Subject { get; set; }
}
and a contoller:
irfwebpage20161013070934_dbEntities2 db = new irfwebpage20161013070934_dbEntities2(); //dbcontext class
List<SubjectOverviewViewModel> list = new List<SubjectOverviewViewModel>(); // to hold list of Customer and order details
var customerlist = (from x in db.Subject
join c in db.SubjectContent on x.NeptunId equals c.NeptunId
select new SubjectOverviewViewModel { NeptunId=c.NeptunId,
TaAdatok=c.TaAdatok,
TaFoAdatok=c.TaFoAdatok,
IrodalomLista=c.IrodalomLista,
TaKurzusok=c.TaKurzusok });
In the select new SubjectOverviewViewModel section i get the error that for example c.TaAdatok is string and it cannot implicitly convert it into model type. The First arguemnt NeptunID doesn't show any error although it is also a string type just like the others. Please help I'm stuck here. I looked at other threads on StackOverflow but those solutions I already implemented and didn't work.
public partial class SubjectContent
{
public string NeptunId { get; set; }
public string TaFoAdatok { get; set; }
public string TaAdatok { get; set; }
public string TaOktatok { get; set; }
public string TaKurzusok { get; set; }
public string IrodalomLista { get; set; }
public virtual Subject Subject { get; set; }
}
public partial class Subject
{
public string NeptunId { get; set; }
public string Name { get; set; }
public virtual Node Node { get; set; }
public virtual SubjectContent SubjectContent { get; set; }
public virtual SubjectRating SubjectRating { get; set; }
public static implicit operator Subject(string v)
{
throw new NotImplementedException();
}
}
Subject and SubjectContent are created by database-first method of entity data modelling.
The problem is that here:
var customerlist = (from x in db.Subject
join c in db.SubjectContent on x.NeptunId equals c.NeptunId
select new SubjectOverviewViewModel { NeptunId=c.NeptunId,
TaAdatok=c.TaAdatok,
TaFoAdatok=c.TaFoAdatok,
IrodalomLista=c.IrodalomLista,
TaKurzusok=c.TaKurzusok });
c.TaAdatok is a string but SubjectOverviewViewModel.TaAdatok is of type SubjectContent.
I think the property types in your view model are wrong. Try this:
public class SubjectOverviewViewModel
{
[DisplayName("Neptunkód")]
public string NeptunId { get; set; }
public string TaFoAdatok { get; set; }
public string TaAdatok { get; set; }
public string TaOktatok { get; set; }
public string TaKurzusok { get; set; }
public string IrodalomLista { get; set; }
}
You can also greatly simplify your query because, as #GertArnold saind in the comments, you already have a SubjectContent navigation property on Subject:
var customerlist = from s in db.Subject
let sc = s.SubjectContent
select new SubjectOverviewViewModel
{
NeptunId = sc.NeptunId,
TaAdatok = sc.TaAdatok,
TaFoAdatok = sc.TaFoAdatok,
IrodalomLista = sc.IrodalomLista,
TaKurzusok = sc.TaKurzusok
};
As a last remark, why do you call the variable customerlist even though it contains data about subjects?
I have DInfo class present in two different namespaces i.e. ABC.Domain and ABC.Common
I am getting xml body as a record from database from which I am deserializing to respective type.
I have to find out all the records which are using properties with name/names of properties of type ABC.Domain.DInfo and just ignore of type ABC.Common.DInfo
As a I am getting record of type IEvent i.e may be FSubmitted or GSubmitted
namespace ABC.Domain
{
public class DInfo
{
public DateTime? Date { get; set; }
public URef User { get; set; }
public Decimal? L1 { get; set; }
public Decimal? L2 { get; set; }
}
}
namespace ABC.Common
{
public class DInfo
{
public DateTime? Date { get; set; }
public URef User { get; set; }
public Decimal? L1 { get; set; }
public Decimal? L2 { get; set; }
}
}
public class Event : IEvent
{
public Guid Id { get; set; }
public Event() { }
public int Number { get; set; }
}
public interface IEvent : IRBase
{
Guid Id { get; set; }
int Number { get; set; }
}
public interface IRBase
{
string RUser { get; set; }
string Sub { get; set; }
}
public abstract class REventBase : Event
{
public Guid Id { get; set; }
}
public class FSubmitted : REventBase
{
public RSummary NewForm { get; set; }
}
public class GSubmitted : REventBase
{
public FRef NewForm { get; set; }
}
public class RSummary
{
public Guid ID { get; set; }
public FRef FRef { get; set; }
public ABC.Common.DInfo Submitted { get; set; }
public ABC.Common.DInfo Saved { get; set; }
public ABC.Domain.DInfo Signed { get; set; }
}
public class FRef : NIdentifier<Guid>
{
public FormType Type { get; set; }
public Version Version { get; set; }
public ABC.Common.DInfo Submitted { get; set; }
public ABC.Domain.DInfo Saved { get; set; }
}
As long as I understand you, you want to get the namespace of the object and then decide what to do with it. If this is the case, this should help.
public static List<IndianAppStore_GetAllAppsByLanguage_ResultCache> GetAllApps(bool initialized, string language)
{
List<IndianAppStore_GetAllAppsByLanguage_ResultCache> objApp = new List<IndianAppStore_GetAllAppsByLanguage_ResultCache>();
List<IndianAppStore_GetAllAppsByLanguage_Result> objApps = new List<IndianAppStore_GetAllAppsByLanguage_Result>();
if (initialized == false)
{
var t = ListCopy(objApps, x => (IndianAppStore_GetAllAppsByLanguage_ResultCache)x); // Error
objApp = admin.getAllAppsByLanguage(language).ToList();
}
else
{
}
}
public static List<TResult> ListCopy<TSource, TResult>(List<TSource> input, Func<TSource, TResult> convertFunction)
{
return input.Select(x => convertFunction(x)).ToList();
}
My Class
public class IndianAppStore_GetAllAppsByLanguage_ResultCache
{
public long AppId { get; set; }
public string AppName { get; set; }
public string AppDisplayName { get; set; }
public string AppDetails { get; set; }
public string AppImageURL { get; set; }
public byte[] AppImageData { get; set; }
public long CategoryId { get; set; }
public Nullable<long> SubCategoryId { get; set; }
public string AppCreatedBy { get; set; }
public System.DateTime AppCreatedOn { get; set; }
public string AppModifiedBy { get; set; }
public Nullable<System.DateTime> AppModifiedOn { get; set; }
public Nullable<bool> isDeleted { get; set; }
public Nullable<bool> isPromotional { get; set; }
public string GenderTarget { get; set; }
public Nullable<long> CountryId { get; set; }
public Nullable<long> StateId { get; set; }
public Nullable<long> AgeLimitId { get; set; }
public Nullable<int> AppMinAge { get; set; }
public Nullable<int> AppMaxAge { get; set; }
}
I am trying to convert one generic class to another but getting this error
IndianAppStore_GetAllAppsByLanguage_Result and IndianAppStore_GetAllAppsByLanguage_ResultCache are different types and you cannot cast the first type to the other as you are doing in this statement:
var t = ListCopy(objApps, x => (IndianAppStore_GetAllAppsByLanguage_ResultCache)x);
If the types have the same structure you should probably just have one instead of two types. Otherwise you will have to copy the data from the first type to the other. E.g.:
var t = ListCopy(objApps, x => new IndianAppStore_GetAllAppsByLanguage_ResultCache {
AppId = x.AppId,
AppName = x.AppName,
...
});
This becomes tedious very quickly and one option is to use a library like AutoMapper to automate the process.