开发者

Using anonymous types - how to avoid lot of code

I have this situation:

if (condition){
   var variable = (from el in ....) //linq to sql query
 }
else{
   var variable = (from el in ....) //linq to sql query
 }

// code where i can`t use the variable

I want to avoid copying code in both conditions.

var getHistoryTips = (from el in objDC.tickets
                      where el.typeOfGame == cANDu[0]
                      && el.username ==开发者_开发技巧 Membership.GetUser(cANDu[1]).ProviderUserKey.ToString()
                      select new { el.AllGamesTickets, el.WGamesTickets}).FirstOrDefault();


var getHistoryTips = (from el in objDC.tickets
                      where el.typeOfGame == cANDu[0]
                      && el.results != null
                      && el.username == Membership.GetUser(cANDu[1]).ProviderUserKey.ToString()
                      select new { el.AllGamesTickets, el.WGamesTickets}).FirstOrDefault();


Three options:

Firstly, you can use an "example" - to type the variable:

// Same anonymous type properties and types as later:
var variable = Enumerable.Repeat(0, new { ... });

if (condition) {
    variable = ...;
} else {
    variable = ...;
}

Alternatively, you could use the conditional operator:

var variable = condition ? ... first query ...
                         : ... second query ...

Thirdly, you could build the query up using composition instead. For example, if the only difference is the ordering, you could do:

var firstPart = ...;

var secondPart = condition ? query.OrderBy(...) : query.OrderByDescending(...);

var query = secondPart.Select(...);

EDIT: Now you've given an example, we can make this concrete:

string username = membership.GetUser(cANDu[1]).ProviderUserKey.ToString();
var query = objDC.tickets
                 .Where(el => el.typeOfGame == cANDu[0] &&
                              el.username == username);

if (condition)
{
    query = query.Where(el => el.results != null);
}

var result = query.Select(el => new { el.AllGamesTickets, el.WGamesTickets})
                  .FirstOrDefault();


You might be able to use the ternary operator:

var variable = (condition) ? (from el in...) : (from el in...);

Where the first linq-to-sql statement (before the colon) is the true case and the linq-to-sql after the colon is the false case. So your code might look like:

var getHistoryTips = (condition) ? (from el in objDC.tickets
                                     where el.typeOfGame == cANDu[0]
                                     && el.username == Membership.GetUser(cANDu[1]).ProviderUserKey.ToString()
                                     select new { el.AllGamesTickets, el.WGamesTickets}).FirstOrDefault() : 
                                   (from el in objDC.tickets
                                     where el.typeOfGame == cANDu[0]
                                     && el.results != null
                                     && el.username == Membership.GetUser(cANDu[1]).ProviderUserKey.ToString()
                                     select new { el.AllGamesTickets, el.WGamesTickets}).FirstOrDefault();


Use your condition in the query itself in the where clause.

You may have to write something like,

var variable = (from el in .... where (condition && trueBlockCondition) || (!condition && falseBlockCondition) 


Functionality you are asking for can be implemented using extensions method, instead of LINQ syntax.

Here are the example:

var source = objDC.tickets.Where(el => 
    el.typeOfGame == cANDu[0]
    && el.username == Membership.GetUser(cANDu[1]).ProviderUserKey.ToString());

if(condition)
    source = source.Where(el => el.results != null);

var getHistoryTips = source
    .Select(el => new { el.AllGamesTickets, el.WGamesTickets}))
    .FirstOrDefault();

If query getting complicated, as an option, selector can be implemented via Expression feature.

Expression<Func<ObjDCType, ShallowObjDCType>> selector = 
   (el) => new ShallowObjDC{ el.AllGamesTickets, el.WGamesTickets});

var getHistoryTips = source.Select(selector).FirstOrDefault();

This technique will allow you yo pass selector inside other function, in case things are getting more complex, however it requires you to define additional class or use dynamic, instead of anonymous classes.

Note, that you can't use Func without Expression, since this will cause ORM (LINQ/EF) to pull all object from database.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜