开发者

Cannot implicity convert type

This is not the first time I have been stuck on collections. I'm missing something here and feeling not just a little frustrated. This code snippet is designed return the count of the number of开发者_开发百科 'restaurant reviews' in the REVIEW db that have a specified 'restaurant id.' Again I'm getting the a "cannot implicitly covert type" error.

Thanks in advance!

    public IEnumerable<string> getNumReviews(int RestID)
    {
        var NumReviews = from REVIEW in db.REVIEWs
                         where REVIEW.REST_ID == RestID
                         group REVIEW by REVIEW.REVIEW_ID into t
                         select new { REVIEW_ID = t.Key, TagCount = t.Count() };
        return NumReviews;
    }


Your method is supposed to return IEnumerable<string> but your code is returning a collection of anonymous objects. Obviously the two aren't the same.

It looks like you need to create a concrete type instead of an anonymous type, and then modify your method to return the appropriate collection:

public class ConcreteType
{
    public string ReviewId { get; set; }
    public int TagCount { get; set; }
}

And then change the method:

public IEnumerable<ConcreteType> GetNumReviews(int restId)
{
    return from REVIEW in db.REVIEWs
           where REVIEW.REST_ID = restId
           group REVIEW by REVIEW.REVIEW_ID into t
           select new ConcreteType 
           { 
               ReviewId = t.Key, 
               TagCount - t.Count()
           };
}


NumReviews is an IEnumerable<anonymous type>, not an IEnumerable<string>. In particular, you're returing an enumeration of objects that include a REVIEW_ID and a count of the tags for each review.

Your best option is to declare a type to encapsulate that information:

public class NumReviewInfo
{
    public int ReviewId { get; set; }
    public int NumTags { get; set; }
}

Then, select that object from the method:

public IEnumerable<NumReviewsInfo> getNumReviews(int RestID)
{
    var NumReviews = from REVIEW in db.REVIEWs
                     where REVIEW.REST_ID == RestID
                     group REVIEW by REVIEW.REVIEW_ID into t
                     select new NumReviewsInfo { ReviewId = t.Key, NumTags = t.Count() };
    return NumReviews;
}


The problem is your NumReviews collection is typed to an IEnumerable<anonymous type> but it's being used as the return of a function that is typed to IEnumerable<string>. You need to either

  • Change the select statement to return a string
  • Change the return type and select statement to produce a concrete type

For example

struct Data { 
  internal int REVIEW_ID;
  internal int TagCount;
}

public IEnumerable<Data> getNumReviews(int RestID) {
    var NumReviews = from REVIEW in db.REVIEWs
                     where REVIEW.REST_ID == RestID
                     group REVIEW by REVIEW.REVIEW_ID into t
                     select new Data { REVIEW_ID = t.Key, TagCount = t.Count() };
    return NumReviews;
}


You aren't guaranteed to get a collection that is enumerable from a query. You'll have to call .ToList() on it to get a List, which is enumerable.


You're returning an anonymous type by doing:

select new { REVIEW_ID = t.Key, TagCount = t.Count() };

Which is a complex type. Whereas, your method signature is expecting a collection of strings.

You can either:

select REVIEW_ID = t.Key

which will match your current method signature, or simply change your method signature to return a complex type (Perhaps a Tuple<string,int> ?) and:

public IEnumerable<Tuple<string,int>> getNumReviews(int RestID)
{
    return
    (
        from REVIEW in db.REVIEWs
        where REVIEW.REST_ID == RestID
        group REVIEW by REVIEW.REVIEW_ID into t
        select new Tuple<string,int>( t.Key, t.Count() );
    );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜