简体   繁体   English

如何编写此通用类型的C#代码段?

[英]How to code this generic type C# snippet?

Simplified repeated code for each entity type is 每个实体类型的简化重复代码为

public IList<entity1> GetEntity1(.. query params ..)
{
    IQueryable<entity1> query = context.entity1;

    query = from refDataType in query
            where refDataType.Id > 0
            select refDataType;
    .
    . plus more changes to query same for each entity
    .

    return query.ToList();
}

I wanted to create a generic function that creates the query, but not sure how to go about it? 我想创建一个创建查询的泛型函数,但不确定如何进行查询吗?

ie in the following snippet, How do I code for ReturnAGenericQuery? 即在以下代码段中,如何为ReturnAGenericQuery编码?

 public IList<entity1> GetEntity1(.. query params ..)
{
    IQueryable<entity1> query = context.entity1;    
    query = ReturnAGenericQuery of type entity1
    return query.ToList();
}

public IList<entity2> GetEntity2(.. query params ..)
{
    IQueryable<entity2> query = context.entity2;    
    query = ReturnAGenericQuery of type entity2
    return query.ToList();
}

private IQueryable<T> ReturnAGenericQuery<T> ()
{
    return IQueryable of entity1 or entity2
}

Your example is a little vague, but it looks like you need something along the lines of: 您的示例有点模糊,但看起来您需要遵循以下要求:

private IQueryable<T> ReturnAGenericQuery<T>(IQueryable<T> source)
    where T : SomeBaseTypeForAllYourEntities
{
    IQueryable<T> result =
        from refDataType in source
        where refDataType.Id > 0
        select refDataType;

    // Other stuff here

    return result;
}

public IList<Entity1> GetEntity1( ... )
{
    return ReturnAGenericQuery(context.entity1).ToList();
}

The reason you need the ' where T : ' clause is because T needs to be a type that has a property ' Id ' for your LINQ where-clause to work ... so you'd need to derive Entity1 and Entity2 from a base class that defines that property. 之所以需要' where T : '子句,是因为T必须是具有LINQ where子句才能工作的属性' Id '的类型,所以您需要从基数派生Entity1和Entity2。定义该属性的类。 If you need any other properties for the 'other stuff' these will need to be added to the base class too. 如果您需要“其他内容”的任何其他属性,那么这些属性也需要添加到基类中。

Addendum: 附录:
If context.entity1 (whatever collection that refers to) is not an IQueryable<entity1> , then you may need to use context.entity1.AsQueryable() instead. 如果context.entity1 (无论引用什么集合)都不是IQueryable<entity1> ,则可能需要使用context.entity1.AsQueryable()代替。

Originally my query was wrong, it was supposed to query from refDataType in source rather than in result ... duh. 最初我的查询是错误的,应该from refDataType in source而不是in result from refDataType in source查询... duh。

Provided you have the right kind of inheritance structure (see below), this compiles fine. 如果您具有正确的继承结构(请参见下文),则可以正常编译。

public class SomeBaseTypeForAllYourEntities
{
    public int Id { get; set; }
}

sealed public class Entity1 : SomeBaseTypeForAllYourEntities
{
    ... other properties, etc. ...
}

You need an interface. 您需要一个接口。

private IQueryable<T, R> ReturnAGenericQuery<T> (T entity) where T : IQueryable, IHasRefDataType
{
    return from DataType refDataType in entity
           where refDataType.Id > 0
           select refDataType;
}

struct DataType
{
    public int Id;
}

public interface IHasRefDataType
{
    DataType refDataType;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM