开发者

Translate SQL query without FROM clause to LINQ

How to translate query like "select 1, 2" (i.e. without FROM clause) to LINQ statement?

Thanks!

I need to get permissions for a set of user groups. In SQL it looks like

SELECT * 
FROM Permission p 
INNER JOIN (SELECT GroupID 
            FROM [Group] 
            UNION ALL 
            SELECT 555) AS g 
ON (g.GroupID = p.GroupID) 
开发者_开发技巧

In my case I need to programmatically add a certain code instead "555". I wouldn't like to write special SQL function for that.


I guess you just want to create an anonymous type

var anonymous = new { Column1 = 1, Column2 = 2 };

Edit - Based on Comments

Depending on what your Select projection is you could do something simple like this:

If it is a Int:

var query = (from per in context.permissions
             select per).AsEnumerable()
             .Concat( new int[] { 1, 2 });

If it is a 'Class'

var query = (from per in context.permissions
             select per).AsEnumerable()
             .Concat(new CustomClass[] 
                      { 
                        new CustomClass()
                        {
                           Prop1= 1
                        },
                      }
                     );

You could also change .Concat to .Union


Why do you need this to be linq?

var numbers = new int[] { 1, 2 };

I suppose

var numbers = Enumerable.Range(1,2);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜